Filtering a matrix to some small matrices
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Moe
le 14 Juil 2014
Modifié(e) : Matz Johansson Bergström
le 14 Juil 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 commentaires
Azzi Abdelmalek
le 14 Juil 2014
I don't understand why A = [2,7;3,5;4,9] , it should be A = [2,7;3,5]
Réponse acceptée
Azzi Abdelmalek
le 14 Juil 2014
Modifié(e) : Azzi Abdelmalek
le 14 Juil 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)
0 commentaires
Plus de réponses (1)
Matz Johansson Bergström
le 14 Juil 2014
Modifié(e) : Matz Johansson Bergström
le 14 Juil 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!