Filtering a matrix to some small matrices

1 ビュー (過去 30 日間)
Moe
Moe 2014 年 7 月 14 日
Hi everyone,
Suppose I have a matrix:
a = [12,7;12,5;2,7;23,3;23,43;23,12;3,5;76,21;76,31;4,3;4,7;4,9];
Then I want to have some sub-divide matrix like following matrixes:
A = [2,7;3,5;4,9]
B = [12,7;12,5;76,21;76,31]
C = [23,3;23,43;23,12;4,3;4,7;4,9]
If first row in matrix a repeated once (e.g. 2-3-4), then that row goes to matrix A If first row in matrix a repeated twice (e.g. 12-76), then that row goes to matrix B If first row in matrix a repeated triple (e.g. 23-4), then that row goes to matrix C ...
Repeation frequency is from 1 to 6 times.
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 14 日
I don't understand why A = [2,7;3,5;4,9] , it should be A = [2,7;3,5]

サインインしてコメントする。

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 14 日
編集済み: Azzi Abdelmalek 2014 年 7 月 14 日
a = [12,7;12,5;2,7;23,3;23,43;23,12;3,5;76,21;76,31;4,3;4,7;4,9]
c1=a(:,1)
[ii,jj,kk]=unique(c1,'stable')
aa=histc(kk,1:numel(jj))
for k=1:max(aa)
jdx=ii(ismember(aa,k))
jj=ismember(c1,jdx)
out{k}=a(jj,:)
end
celldisp(out)

その他の回答 (1 件)

Matz Johansson Bergström
Matz Johansson Bergström 2014 年 7 月 14 日
編集済み: Matz Johansson Bergström 2014 年 7 月 14 日
I am sure there is a function in statistics that does this for you, but I quickly wrote a very dirty version using cells, just to handle the matrices in a nice way. You can replace the cells if you like. I assume that the highest frequency is 6, as you state.
Tally = {}; %the matrices
for i = 1:6 %assume frequency is at most 6
Tally{i} = []; %"allocate"
end
[un, in] = unique(a(:, 1));
%Instead of fixing the number of matrices, we store them in a cell and
%count the number of repeats
for i = 1:size(un, 1)
num = un(i); %the number we are counting on at the moment
inds = find( a(:, 1) == num ); %the indices where 'num' is
freq = length(inds);
Tally{freq} = [Tally{freq}; a(inds, :)];
%append the new coordinate in index 'freq'
end
So, a very quick and dirty solution. If I run it on your sample and print my cell Tally, we get
>Tally{:}
ans =
2 7
3 5
ans =
12 7
12 5
76 21
76 31
ans =
4 3
4 7
4 9
23 3
23 43
23 12
ans =
[]
ans =
[]
ans =
[]
I also noticed that [4,9] should not be in A. Hope this helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by