필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Erase all the rows that contain NaN

조회 수: 1 (최근 30일)
Sabbas
Sabbas 2012년 7월 18일
마감: MATLAB Answer Bot 2021년 8월 20일
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]

답변 (3개)

Miro
Miro 2012년 7월 18일
편집: Miro 2012년 7월 18일
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
Sabbas
Sabbas 2012년 7월 18일
편집: Sabbas 2012년 7월 18일
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 2012년 7월 18일
편집: Miro 2012년 7월 18일
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 2012년 7월 18일
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
Sabbas
Sabbas 2012년 7월 18일
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 2012년 7월 18일
Rout = R(~all(cellfun(@(x)all(isnan(x)),R),2),:)
  댓글 수: 4
Jan
Jan 2012년 7월 18일
@Sabbas: Do you get a "warning" or an "error"? In both cases posting the message would be a good idea.
Sabbas
Sabbas 2012년 7월 20일
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

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by