Monday, November 28, 2011

remove any rows that contains a specific number in Matlab

In this post, I am trying to solve the problem given in the comments of one of the old post.
Here's the problem, if I understand it correctly:

a =

     1     2     3     4     5
     2     3     4   -99     6
     3     4     5     6     7
     4   -99     6     7     8
     5     6     7     8     9

How to remove all the rows that contains the number -99, in this case, the row #2 and #4?

And here's the code I wrote to do this job:

[rows col]=size(a); %count the number of rows and columns of matrix a
j=0; %initialize a counter;
for i=1: rows
    if sum(a(i,:)~=-99)==col; %This determines if the row has -99 or not, if not, do nothing
    else     %shift all of the rest of the rows up one row
        for m=i: rows-1; 
        a(m,:)=a(m+1,:);
        end
        j=j+1; %count how many rows has -99, which equals how many times the rows has been shifted up 
    end
end
b=a((1:rows-j),:); %get rid of the last j rows


The result is:



b =


     1     2     3     4     5
     3     4     5     6     7
     5     6     7     8     9



I hope this can work for you, Winifred. Thanks for your comments!

24 comments:

  1. a better solution would be:

    a = [1 2 3 4 5;2 3 4 -99 6;3 4 5 6 7;4 -99 6 7 8;5 6 7 8 9];

    %Remove any row with -99
    a(any(a==-99,2),:) = []


    a = [1 2 3 4 5;2 3 4 -99 6;3 4 5 6 7;4 -99 6 7 8;5 6 7 8 9];
    %Remove any column with -99
    a(:, any(a==-99,1)) = []

    ReplyDelete
    Replies
    1. I'm a new bie and kindly explain me this doubt,

      a(any(a==-99,2),:) = []

      what does -99,2 denotes in this line?

      Delete
    2. -99 is a made up number, in this case, I want to remove the row (or the column) if that row (or column)contains -99. It could be any other number.

      The 2 and 1 in the 'any' function is a parameter that tells the function to look either into rows or into columns. Here's the reference for 'any' function:

      http://www.mathworks.com/help/techdoc/ref/any.html

      Thanks for your post.

      Delete
    3. Hey cguitar,
      Hope you can help me on this. I have a big matrix say,
      MAT[1 1 4
      1 1.5 7
      2 1.2 6
      2 1.6 8]

      How to remove the rows that contains values greater than 1.5 in second column.
      Help me out pls. Thanks

      Delete
    4. You can use Excel to do this job easily.

      Please check the number filter function at this link:

      http://office.microsoft.com/en-us/excel-help/filter-data-in-a-range-or-table-HP010073941.aspx

      Delete
  2. That's really nice! Thank you very much, Rafael!

    ReplyDelete
  3. Hi friends, I am newcomer to Matlab. I have got a big 720x19 matrix in the form of a .mat file. This is the data of wind speed for one full month. I want to plot this. I have to remove the elements when the value is 999.

    I loaded the file as:

    a=load('sep11hznb.mat')

    Now how should I proceed to achieve my goal?

    ReplyDelete
  4. Hi Kutty,

    What do you want to replace the 999 with? If you just want to remove the 999 and leave a 'hole' there, you can use this comand:

    a(a==999)=NaN;

    If you want to replace the 999 with another number, say 0, this will do it:

    a(a==999)=0;

    Good luck!

    ReplyDelete
    Replies
    1. Hello cguitar,

      What if you want to replace a text value like NULL with a value of 0 or any other number value, for example? After reading my matrix in, I tried the same command

      block2(block2==NULL) = 0;

      but it didn't work.

      Thanks for your help

      Delete
    2. Hello Muneer,
      Please try this and let me know if it works.
      block2( strcmp(block2, 'NULL') )=0;
      :)

      Delete
  5. Thank you very much cguitar...Appreciate it..!

    ReplyDelete
  6. I have 10000 data sets of (x,y) for which i need to fit a curve. Once I could fit a curve, i need to get points on the curve with an uniform interval. Then I would proceed to patch..and then to stl for my ultimate objective. Right now i'm struck with the curve fitting. Kindly help me out.

    Link for the description: https://docs.google.com/open?id=0BxIgxUb0m51-ZDA2ZWU0NzItM2JkZS00MWI4LTg0ZTMtNTI0ZjQzMzIxMzU3

    ReplyDelete
    Replies
    1. Sorry I can't help on this one, because I am not familiar with the curve fitting tools and I don't get your question.

      Delete
  7. Cguitar,

    Can you help me with the last question :)

    ReplyDelete
  8. I was looking for a matlab option to do the following.

    I have a big matrix say,
    MAT[1 1 4
    1 1.5 7
    2 1.2 6
    2 1.6 8]

    How to remove the rows that contains values greater than 1.5 in second column.
    Help me out pls. Thanks

    ReplyDelete
  9. You can use Excel to do this job easily.

    Please check the number filter function at this link:

    http://office.microsoft.com/en-us/excel-help/filter-data-in-a-range-or-table-HP010073941.aspx

    ReplyDelete
  10. how can i count the number of deleted rows ? and make them answer appeaar in the command window

    ReplyDelete
  11. how can I divide bin from a big matrix of image

    ReplyDelete
    Replies
    1. I don't quite understand your question. If you can provide more description of your problem or give a sample dataset, that would be helpful.

      Delete
  12. how can i select some points(numbers) in the matrix file

    ReplyDelete
    Replies
    1. say if you have a matrix M. you can use
      a=M(i,j)
      to assign the element in ith row jth column to a.

      I hope this answers your question :)

      Delete
  13. And what if you have words, let's say 'hello', several times in yoir matrix and you want to change this for zeros?

    ReplyDelete
    Replies
    1. Hi Lacor,
      Converting string ('hello') to numbers is trickier. It also depends on if you stored your data in cell array or structure or dataset.
      If you can leave more information about your data, I'd be glad to look into them.

      Delete
  14. Removing rows containing specific criteria is a vital data cleaning operation. Fun Game Ideas In programming or data analysis, it entails identifying and eliminating rows that meet certain conditions.

    ReplyDelete

Any comments?

my-alpine and docker-compose.yml

 ``` version: '1' services:     man:       build: .       image: my-alpine:latest   ```  Dockerfile: ``` FROM alpine:latest ENV PYTH...