Erase all the rows that contain NaN

Dear all
I have
[N,T,R]=xlsread(name)
I want to erase all the rows in R that contain only NaN
thanks
[EDITED, copied from comments]:
So to input cell matrix is
'TOTPAIN' [NaN] [6.9150]
[1x40 char] [NaN] [6.90]
[ NaN] [NaN] [NaN]
[ NaN] [NaN] [NaN]
'MAS' [NaN] [NaN]
'MAR PACK G' [NaN] [6.7]
'MA 3 PACK 58G' [NaN] [6.7]

Antworten (3)

Miro
Miro am 18 Jul. 2012
Bearbeitet: Miro am 18 Jul. 2012

0 Stimmen

hi,
try this
[n,~]=size(R)
R_new = [];
for i = 1 : n
R_tmp = R(i,:);
if length(find(isnan(R_tmp)) ~= length(R_tmp)
R_new = [R_new;R_tmp]
end
end

7 Kommentare

Sabbas
Sabbas am 18 Jul. 2012
Bearbeitet: Sabbas am 18 Jul. 2012
Hi Miro. I want to delete only the ROWS that contain only NaN.
By applying your command I obtain the following error message
Undefined function or method
'isnan' for input arguments of type 'cell'.
thanks
Sabbas
Sabbas am 18 Jul. 2012
Bearbeitet: Sabbas am 18 Jul. 2012
I am looking for something more compact Miro. Thanks anyway
Also, by applying your code I have
Error: Unbalanced or unexpected
parenthesis or bracket.
Sabbas
Sabbas am 18 Jul. 2012
Just to remind you that R is a celll matrix
thanks
Sabbas
Sabbas am 18 Jul. 2012
could anyone help?
Thanks
Jan
Jan am 18 Jul. 2012
@Sabbas: Instead of reminding, it would be helpful if you explain such important details at first. Let me remind you, that the function xrsread is not a common function with known output. Do you mean xlsread?
Sabbas
Sabbas am 18 Jul. 2012
Bearbeitet: Sabbas am 18 Jul. 2012
Hi Simon. Yes, xlsread. I edited my question to correct this mistake So R is a cell matric that contains 'NaN', and other string variables like 'Hello' etc
Miro
Miro am 18 Jul. 2012
Bearbeitet: Miro am 18 Jul. 2012
sorry for the missunderstanding. the following should work Your example looks like you want to delete the Coloumn, not the Row... For deleting the column try the following:
function Rout = deleteNAN(Rin)
[~,n]=size(R)
R_new = {};
for i = 1 : n
R_tmp = cell2mat(R(:,i));
if length(find(isnan(R_tmp))) ~= length(R_tmp)
R_new = [R_new R_tmp]
end
end
Rout = R_new
Store this in a .m file in your current folder and run it.
Jan
Jan am 18 Jul. 2012

0 Stimmen

Perhaps something like this:
index = cellfun(@isequalwithequalnans, R, {NaN});
R(all(index, 2)) = [];
But due to a vague defined input, this contains too much guessing to be reliable. Sorry.

1 Kommentar

thanks simon. I applied you code and I obtain the following error message
Error using ==> cellfun All of the input arguments must be of the same size and shape. Previous inputs had size 376 in dimension 1. Input #3 has size 1.
So to input cell matrix is
'TOTPAIN' [NaN] [6.9150]
[1x40 char] [NaN] [6.90]
[ NaN] [NaN] [NaN]
[ NaN] [NaN] [NaN]
'MAS' [NaN] [NaN]
'MAR PACK G' [NaN] [6.7]
'MA 3 PACK 58G' [NaN] [6.7]
thanks
Andrei Bobrov
Andrei Bobrov am 18 Jul. 2012

0 Stimmen

Rout = R(~all(cellfun(@(x)all(isnan(x)),R),2),:)

4 Kommentare

Sabbas
Sabbas am 18 Jul. 2012
thanks Andrei. It does not work for the whole matrix. I get no warning but the rows with only NaN are not deleted
Andrei Bobrov
Andrei Bobrov am 18 Jul. 2012
Bearbeitet: Andrei Bobrov am 18 Jul. 2012
R = {...
'TOTPAIN' [NaN] [6.915]
'hfffgfjfjgfj' [NaN] [ 6.9]
[ NaN] [NaN] [ NaN]
[ NaN] [NaN] [ NaN]
'MAS' [NaN] [ NaN]
'MAR PACK G' [NaN] [ 6.7]
'MA 3 PACK 58G' [NaN] [ 6.7]};
>> Rout = R(~all(cellfun(@(x)all(isnan(x)),R),2),:)
Rout =
'TOTPAIN' [NaN] [6.915]
'hfffgfjfjgfj' [NaN] [ 6.9]
'MAS' [NaN] [ NaN]
'MAR PACK G' [NaN] [ 6.7]
'MA 3 PACK 58G' [NaN] [ 6.7]
>>
Jan
Jan am 18 Jul. 2012
@Sabbas: Do you get a "warning" or an "error"? In both cases posting the message would be a good idea.
Sabbas
Sabbas am 20 Jul. 2012
Well, I get neither error nor warning. The rows full of NaNs still remain in the matrix. If I run the above codes you kindly provide for a matrix of 3 columns it seems to work

Diese Frage ist geschlossen.

Tags

Gefragt:

am 18 Jul. 2012

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by