How do I remove rows with NaNs from my raw data?

조회 수: 1 (최근 30일)
Tessa
Tessa 2014년 11월 18일
댓글: Guillaume 2014년 11월 25일
I have opened my excel data with xlsread and would like to remove all rows containing a NaN. Is there a possibility to do this at once? my data looks like this:
information blabla NaN NaN information blabla NaN NaN
NaN NaN NaN
Mydata Mydata Mydata My Data Mydata Mydata Mydata My Data Mydata Mydata NaN My Data Mydata Mydata Mydata My Data Mydata Mydata Mydata NaN Mydata Mydata Mydata My Data
It´s a cell array in case that matters :)
  댓글 수: 1
Guillaume
Guillaume 2014년 11월 18일
It does matter greatly that it's a cell array. Other than the NaNs, what's in the cell array? strings?, matrices? or just scalars?

댓글을 달려면 로그인하십시오.

답변 (2개)

Giorgos Papakonstantinou
Giorgos Papakonstantinou 2014년 11월 18일
I assume that your data are of nx1 or 1xn size. Then to delete the use this:
data(isnan(data)) = [];
  댓글 수: 4
Guillaume
Guillaume 2014년 11월 18일
This works as long as each element of the cell array is a scalar.
It won't work on:
data = {'info', [1 2 3 4 NaN], NaN, 5};
Tessa
Tessa 2014년 11월 25일
Thanks for the response. It is a mixture of numbers, dates and text. So both are not working... Is there a way to change the cell array in something numeric eventhough it is not all numbers?

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2014년 11월 25일
Tessa, try this:
c={'sometext', NaN, 12.5
'othertxt', 45, 12
NaN, 'blah', 7
48, 78, 'aaa'} %example
nanrows = any(cellfun(@(e) isnumeric(e) && isnan(e), c), 2);
c(nanrows, :) = []
It will work as long as you don't have a matrix of numbers in a cell. If you do, replace the
isnan(e)
by
any(isnan(e(:)))
  댓글 수: 2
Tessa
Tessa 2014년 11월 25일
I´m feeling like a huge beginner at the moment :/ The result is: nanrows containing a logical filled with ones. And my original cell raw data now being a empty cell. I don´t understand what I´m doing wrong. What does the e mean?
Anyhow, I found a way around it and don´t need to do this anymore, but might be usefull in the future :)
Guillaume
Guillaume 2014년 11월 25일
If you get an empty cell, that means each row must contain at least one NaN.
I find it a bit odd to discard a whole row if there's just one NaN in it, but that's what you requested: "remove all rows containing a NaN".
The @(e) isnumeric(e) && isnan(e) is an anonymous function where e is just a placeholder name for any cell of the cell array. This is equivalent to:
function out = anonymousfunction(e)
out = isnumeric(e) && isnan(e);
end
Obviously, you could use any name you want instead of e.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by