필터 지우기
필터 지우기

Remove rows or cols whose elements are all NaN

조회 수: 246 (최근 30일)
Jeon
Jeon 2013년 3월 25일
댓글: osman alper altun 2023년 2월 14일
How can I remove rows or cols whose elements are all NaN ? Withouot any dirty iterations?
For example,
A = [1 1 1 1 1 1 1 1 1 1;
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN;
1 1 1 1 1 1 1 1 1 1;
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN;];
should turned into
A = [1 1 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 1 1];
This is just an example. Actually I have a very big matrix. So I want a solution of this question to work well with my big matrix.
  댓글 수: 2
Dev-iL
Dev-iL 2014년 7월 13일
편집: Dev-iL 2014년 7월 13일
Hi! You can find some clues here: A discussion of the opposite problem on SE. I personally achieved what you were attempting using:
A(~any(~isnan(A), 2),:)=[];
Walter Roberson
Walter Roberson 2015년 9월 20일
Michal Gajewski commented
works

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 3월 25일
편집: Andrei Bobrov 2013년 3월 25일
out = A(all(~isnan(A),2),:); % for nan - rows
out = A(:,all(~isnan(A))); % for nan - columns
  댓글 수: 5
Céldor
Céldor 2015년 4월 2일
Can I obtain a logical matrix I of indices to be held / removed something like
I = something here
and then use assign an altered matrices:
out1 = out1(I);
out2 = out2(I);
% ... etc.
Thanks
gringer45
gringer45 2018년 3월 18일
This doesn't really do what the question asks for. This selects all the columns or rows with none (zero) NaN values. So, this is answering the question: "Remove rows or cols whose elements have any (at least one) NaN"

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

추가 답변 (6개)

Phillippe
Phillippe 2015년 1월 14일
To remove only ALL-NaN columns, do this instead:
A = A(:,~all(isnan(A)));
  댓글 수: 4
Parth Dev Bundela
Parth Dev Bundela 2022년 10월 22일
Thanks man
osman alper altun
osman alper altun 2023년 2월 14일
Thank you!

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


Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 25일
  댓글 수: 5
Serafeim Zacharopoulos
Serafeim Zacharopoulos 2020년 5월 27일
To delete rows with all NaN's, maybe try this:
A = A(find(sum(~isnan(A)'))',:)
Walter Roberson
Walter Roberson 2020년 5월 28일
A(all(isnan(A),2),:) = []; %rows that are all nan
A(:, all(isnan(A),1)) = []; %cols that are all nan

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


Saman
Saman 2013년 10월 23일
편집: Saman 2013년 10월 23일
Use this :
out = A(:,any(~isnan(A))); % for columns
out = A(any(~isnan(A),2),:); %for rows

Alfaz Memon
Alfaz Memon 2018년 8월 20일
편집: Alfaz Memon 2018년 8월 21일
input varibale : data
output variable : row_index( index of rows with all the value as NaN)
row_index( index of rows with all the value as NaN)
column_index( index of columns with all the value as NaN)
if(iscell(data))
x =find(cell2mat((cellfun(@(data) any(isnan(data),2),data,'UniformOutput',false))));
[ia,ib] = ind2sub(size(data),x);
rows_unique = unique(ia);
rows_unique(:,2)=histc(ia,rows_unique);
row_index = rows_unique(find(rows_unique(:,2)==size(data,2)),1);
columns_unique = unique(ib);
columns_unique(:,2)=histc(ib,columns_unique);
column_index = rows_unique(find(columns_unique(:,2)==size(data,1)),1);
else
row_index =find(~any(~isnan(data), 2)); % row with all NaN values
column_index =find(~any(~isnan(data), 1)); %column with all NaN values
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 8월 20일
Seems like a bit of a bother to just remove the rows or columns ?
I notice that you are using cellfun on the data, implying that the data is a cell array; in the original question it was a plain array.
Alfaz Memon
Alfaz Memon 2018년 8월 21일
편집: Alfaz Memon 2018년 8월 21일
yeah its for cell array. for plain array you can remove cellfun and just simply keep any(isnan(data),2) instead of x =find(cell2mat((cellfun(@(data) any(isnan(data),2),data,'UniformOutput',false))));
this can you work for cell array of different data type.
Also I have updated solution.

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


Ilham Hardy
Ilham Hardy 2013년 3월 25일
Haven't tried this, but it should works:
A(isnan(A))=[];
  댓글 수: 1
Jan
Jan 2013년 3월 25일
This does not solve the wanted: "delete rows or cols whose elements are all NaN"

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


Nike
Nike 2013년 3월 25일
Simplest is
A(isnan(A))= [];
  댓글 수: 1
Jan
Jan 2013년 3월 25일
This has been posted twice already. But it still does not solve the original question:
delete rows or cols whose elements are all NaN
For e.g. A = [1, NaN, 1; NaN, 1, NaN] nothing should be deleted.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by