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

采纳的回答

dpb
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);

更多回答(1 个)

monika shivhare
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}

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by