i want to make the rows of some elements values empty and should be maintain the other row value

6 views (last 30 days)
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

Accepted Answer

dpb
dpb on 17 May 2018
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);

More Answers (1)

monika shivhare
monika shivhare on 18 May 2018
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}

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!