Filtering a matrix to some small matrices

1 Ansicht (letzte 30 Tage)
Moe
Moe am 14 Jul. 2014
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 Kommentare
Image Analyst
Image Analyst am 14 Jul. 2014
"a" is messed up -- the code doesn't run!
Azzi Abdelmalek
Azzi Abdelmalek am 14 Jul. 2014
I don't understand why A = [2,7;3,5;4,9] , it should be A = [2,7;3,5]

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 14 Jul. 2014
Bearbeitet: Azzi Abdelmalek am 14 Jul. 2014
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)

Weitere Antworten (1)

Matz Johansson Bergström
Matz Johansson Bergström am 14 Jul. 2014
Bearbeitet: Matz Johansson Bergström am 14 Jul. 2014
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.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by