MATLAB: Filtering a matrix to some small matrices

accumarrayarraymatrixreshape

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.

Best Answer

  • 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)