How to split a matrix in different sections in a loop?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
MRC
am 13 Feb. 2014
Beantwortet: Jos (10584)
am 13 Feb. 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 Kommentare
Akzeptierte Antwort
Jos (10584)
am 13 Feb. 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 Kommentare
Weitere Antworten (0)
Siehe auch
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!