Remove rows or cols whose elements are all NaN

182 次查看(过去 30 天)
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),:)=[];

请先登录,再进行评论。

采纳的回答

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 个评论

请先登录,再进行评论。


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

Azzi Abdelmalek
Azzi Abdelmalek 2013-3-25
  6 个评论
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
Andrew Sol
Andrew Sol 2024-11-14,7:43
You proposed too simple a solution and you were wrong.

请先登录,再进行评论。


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 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by