i want to make the rows of some elements values empty and should be maintain the other row value
5 次查看(过去 30 天)
显示 更早的评论
example;
m(1)=[1 4 6 8 10 11 17 26 90]; %(first iteration)
m(2)=[2 1 14 11 0 0 0 0 0 ]; %(second iteration)
in second iteration 0 arises but i want 0's to make empty and i want to get
m(2)=[2 1 14 11];
( i am using this code ;
K=find(m(2,:)==0)
m(2)(K)=[];
A=m(2);
getting error, please rectify this one
0 个评论
采纳的回答
dpb
2018-5-17
You can't make m(2,:) be four elements long while m(1) is 9 (or anything other than also four for that matter) as arrays must be rectangular (ergo, not "ragged"). The only thing you can do in m is to make it a cell array of size(m,1) rows where each cell contains the nonzero elements. Or, if you only want the second row specifically and do want a new variable name, then the syntax is just
A=m(m(2,:)~=0);
0 个评论
更多回答(1 个)
monika shivhare
2018-5-18
Hey Rajesh, Based on your query it looks like you are trying to make a 2 dimensional array on size 2*9 and then making zero elements from the 2nd row empty. In MATLAB, matrices are treated as collection of vectors of same type and size. But since you are trying to remove few elements from 2nd row of the matrix while keeping the size of 1st row intact, it is showing an error because the 1st and 2nd row no longer remains of the same size. To fix this, you can define m as a cell array and then make the manipulations as required.
m{1}=[1 4 6 8 10 11 17 26 90];
m{2}=[2 1 14 11 0 0 0 0 0 ];
K=find(m{2}==0) ;
m{2}(K)=[];
A=m{2}
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!