Filtering a matrix to some small matrices

조회 수: 1 (최근 30일)
Moe
Moe 2014년 7월 14일
편집: Matz Johansson Bergström 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
Image Analyst
Image Analyst 2014년 7월 14일
"a" is messed up -- the code doesn't run!
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.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by