How to split a matrix in different sections in a loop?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!