Extract row elements corresponding to a particular column value and save as new matrix
이전 댓글 표시
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
2013년 3월 14일
편집: Azzi Abdelmalek
2013년 3월 14일
b=A(:,3)
b(b==c)=yourvalue
A(:,3)=b
댓글 수: 17
Sarah
2013년 3월 14일
Azzi Abdelmalek
2013년 3월 14일
How do you want to replace these c numbers?
Sarah
2013년 3월 14일
Sarah
2013년 3월 14일
Azzi Abdelmalek
2013년 3월 14일
편집: Azzi Abdelmalek
2013년 3월 14일
If A=[ 2 3 4 6
4 8 7 9
5 2 7 1
4 2 2 3]
What should be the result?
Azzi Abdelmalek
2013년 3월 14일
편집: Azzi Abdelmalek
2013년 3월 14일
A=[ 2 3 4 6
4 8 7 9
5 2 7 1
4 2 2 3]
A=sortrows(A,3)
b=A(:,3);
c=unique(b);
for k=1:numel(c)
idx=find(b==c(k));
assignin('base',sprintf('mat%d',c(k)),A(idx,:))
end
%Check the matrices mat2, mat4 and mat7
Sarah
2013년 3월 14일
Azzi Abdelmalek
2013년 3월 14일
But if you have three values of c: 1.8, 2 and 2.6 how will you round?
Azzi Abdelmalek
2013년 3월 14일
편집: Azzi Abdelmalek
2013년 3월 14일
What I suggest is using cell array
b=A(:,3);
c=unique(b);
for k=1:numel(c)
idx=find(b==c(k));
out{k}=A(idx,:)
end
then check
out{1}
out{2}
out{3}
Sarah
2013년 3월 14일
Azzi Abdelmalek
2013년 3월 14일
What about the cell array solution?
Sarah
2013년 3월 14일
Azzi Abdelmalek
2013년 3월 14일
편집: Azzi Abdelmalek
2013년 3월 14일
Did you check the cell array solution? You do not need to create such variables
Sarah
2013년 3월 14일
Azzi Abdelmalek
2013년 3월 14일
Check this :
clear out
b=A(:,3);
c=unique(b);
for k=1:numel(c)
idx=find(b==c(k));
out{k,2}=A(idx,:)
out{k,1}=c(k)
end
out{1,1}, % is c1
out{1,2} % corresponding to c1
out{2,1}, % is c2
out{2,2} % corresponding to c2
and so on
Sarah
2013년 3월 14일
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
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!