Monday, March 16, 2015

remove any rows that contains a specific number in Matlab

On this submit, I'm making an attempt to unravel the issue given within the feedback of one of many previous submit.
This is the issue, if I perceive it appropriately:

a =

     1     2     three     four     5
     2     three     four   -ninety nine     6
     three     four     5     6     7
     four   -ninety nine     6     7     eight
     5     6     7     eight     9

Methods to take away all of the rows that accommodates the quantity -ninety nine, on this case, the row #2 and #four?

And this is the code I wrote to do that job:

[rows col]=measurement(a); %rely the variety of rows and columns of matrix a
j=zero; %initialize a counter;
for i=1: rows
    if sum(a(i,:)~=-ninety nine)==col; %This determines if the row has -ninety nine or not, if not, do nothing
    else     %shift all the remainder of the rows up one row
        for m=i: rows-1; 
        a(m,:)=a(m+1,:);
        finish
        j=j+1; %rely what number of rows has -ninety nine, which equals what number of occasions the rows has been shifted up 
    finish
finish
b=a((1:rows-j),:); %eliminate the final j rows

The result's:


b =

     1     2     three     four     5
     three     four     5     6     7
     5     6     7     eight     9



I hope this could be just right for you, Winifred. Thanks in your feedback!

No comments:

Post a Comment