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

1 回表示 (過去 30 日間)
MRC
MRC 2014 年 2 月 13 日
回答済み: Jos (10584) 2014 年 2 月 13 日
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!

採用された回答

Jos (10584)
Jos (10584) 2014 年 2 月 13 日
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 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by