Sunday, June 21, 2009

Matlab: Remove the NaN elements

Recently I got a very long column of data and it contains lots of NaN. I found the finite function very useful to help me remove all the NaN elements. Also it can remove the Inf elements. For example:
C1=[0 1 2 3 NaN 4 5 6 NaN 7 Inf 8 Inf 9];
C2=C1(finite(C1));

we get :
C2=[0 1 2 3 4 5 6 7 8 9]

I'm using R2008a version and I got a warning like this:
Warning: FINITE is obsolete and will be removed in future versions. Use ISFINITE instead.

20 comments:

  1. hey that ISFINITE function is really helpful. thanks for pointing that out !!

    ReplyDelete
  2. hiii, do you know how to apply that isinfinite to remove NaN elements in a matrix??

    I appreciate your help!!!!

    ReplyDelete
  3. C1=[0 1 2 3 NaN 4 5 6 NaN 7 Inf 8 Inf 9];
    C2=C1(isfinite(C1));

    But this will also remove the Inf elements. Do you mean you want to keep those Inf elements, just remove NaN elements?

    ReplyDelete
  4. I have several columns and rows. When I use isfinite, it arranges everything into just one column. How do I eliminate the NaN without messing with the column arrangements? Or is there a function that allows me to ignore the NaN cells when I'm processing the data. Because then, I can just create an "if" statement.

    ReplyDelete
    Replies
    1. This link might solve your problem
      http://stackoverflow.com/questions/5202680/matlab-how-to-efficiently-remove-nan-elements-from-matrix

      Delete
  5. Hi FrdA, sorry I still haven't come up with a good solution to your problem.
    I don't know why the matrix will be converted into a single column.

    ReplyDelete
  6. to remove NaN

    c1=(isnan(c1))=0;

    this will place a zero where Nan were.

    Hope this helps

    ReplyDelete
  7. oops

    the command should be

    c1(isnan(c1))=0

    sorry
    TTKK

    ReplyDelete
  8. how to remove inf?

    ReplyDelete
  9. i have a matrix.. i need to take average of 8 neighbouringg pixels. but there are some NaN values.. how can i ignore them while taking average.. ??
    thanks in advance.. :)

    ReplyDelete
    Replies
    1. Hi Saksham, you can use the nanmean function.

      Delete
  10. thanks...
    but now i m facing a very simple problem.. need help..
    In an image i want to assign only two colors to pixels, one with value greater than 0 and other with value less than 0... i.e say black with pixel value less than o.. and white for greater than 0..
    thanks..

    ReplyDelete
  11. hi
    how to remove nan from excel sheet already read in matlab?

    ReplyDelete
    Replies
    1. The post is talking about how to use matlab, not excel.

      Delete
  12. This comment has been removed by the author.

    ReplyDelete

Any comments?

my-alpine and docker-compose.yml

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