Extract row elements corresponding to a particular column value and save as new matrix
2 次查看(过去 30 天)
显示 更早的评论
Sarah
2013-3-14
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
Thank you! the values of c are increasing but not monotonically, I would like to create a loop to extract these values corresponding to the different c values. Any ideas on how I might do that?
Sarah
2013-3-14
I do not want to replace them, I just want to extract the data corresponding to the different groups of c values for example create a matrix containing the data corresponding to c1 values and so on.
Sarah
2013-3-14
Also in the code you wrote there the last line just rewrites the variable b with the original matrix again?
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
And because some of the c values are not integers is there a way to round the c value before assigning it to the variable name?
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
I would round to the nearest integer eg 1.8->2 and so on.. Finally to save each matrix, I am unfamiliar with the assignin function. How would I save the individual matrices eg mat11 mat12 and so on..?
Sarah
2013-3-14
I have looked at the various matrices when I round the value c to the nearest integer, it is just saving the matrices within the loop that I am having some difficulty with now.
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
You mean I do not need to save the matrices? I would like to save them individually as they are created within the loop.
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
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
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
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 Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)