Extract row elements corresponding to a particular column value and save as new matrix

조회 수: 2 (최근 30일)
I have a Mx4 matrix with the following format:
a1 b1 c d1
a2 b2 c d2
a3 b3 c d3
a4 b4 c1 d4
a5 b5 c1 d5
and what i want to do is create new matrices corresponding to the different c values in the third column renaming this matrix matc.
Any help would be really appreciated Sarah

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 14일
편집: Azzi Abdelmalek 2013년 3월 14일
b=A(:,3)
b(b==c)=yourvalue
A(:,3)=b
  댓글 수: 17
Sarah
Sarah 2013년 3월 14일
the matrices are being created correctly with the original code, I only wish to now save each matrix that was created within the loop eg after the assignin('base',sprintf('mat%d',c(k)),A(idx,:)) line. I appreciate your help with this!
Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 14일
편집: Azzi Abdelmalek 2013년 3월 14일
% Check this: for c=2.4 the variable will be mat2p4 instead of mat2.4
A= [ 2 3 4 6
4 8 7 9
5 2 7 1
4 2 2.4 3]
A=sortrows(A,3)
b=A(:,3);
c=unique(b);
for k=1:numel(c)
idx=find(b==c(k));
s=num2str(c(k))
s=strrep(s,'.','p')
assignin('base',['mat' s],A(idx,:))
end
Check the result
mat2p4
mat4
mat7

댓글을 달려면 로그인하십시오.


Sarah
Sarah 2013년 3월 14일
So the matrix is sorted according to the third column so
A1=[4 2 2 3
2 3 4 6
4 8 7 9
5 2 7 1];
the answers should be
mat2=[4 2 2 3];
mat4=[2 3 4 6];
mat7=[4 8 7 9
5 2 9 1];

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by