How to split a matrix in different sections in a loop?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MRC
le 13 Fév 2014
Réponse apportée : Jos (10584)
le 13 Fév 2014
Hi all, I have to pick a particular section of a matrix A in each iteration of a loop and use that section for getting some results, i.e.
clear all
A=[ 1 2 3 1; 4 5 6 1; 2 3 4 2; 5 6 7 2; 8 9 3 2; 5 1 2 4; 7 8 1 4];
a=unique(A(:,4));
for j=1:size(a)
rho=a(j);
% B=... for j=1, a(1)=1, then I should select B=[1 2 3 1; 4 5 6 1];
%for j=2, a(2)=2, then I should select B=[2 3 4 2; 5 6 7 2; 8 9 3 2];
%for j=3, a(3)=4, then I should select B=[5 1 2 4; 7 8 1 4];
%use B to compute some quantities...
end
The last column of B has values in ascending order, not necessarily equidistant among each other and not necessarily repeated the same number of times. I would like to avoid loops. Could you help me? Thanks!
0 commentaires
Réponse acceptée
Jos (10584)
le 13 Fév 2014
A=[ 1 2 3 1 ;
4 5 6 1 ;
2 3 4 2 ;
5 6 7 2 ;
8 9 3 2 ;
5 1 2 4 ;
7 8 1 4 ];
UniqueA=unique(A(:,4))
for j=1:numel(UniqueA)
tf = A(:,4) == UniqueA(j)
tmpB = A(tf,:) % select from A
% .. some calculations on tmpB here ..
end
0 commentaires
Plus de réponses (0)
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!