How to split a matrix in different sections in a loop?

1 view (last 30 days)
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!

Accepted Answer

Jos (10584)
Jos (10584) on 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

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!