Remove rows or cols whose elements are all NaN

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 日

24 投票

out = A(all(~isnan(A),2),:); % for nan - rows
out = A(:,all(~isnan(A))); % for nan - columns

6 件のコメント

Jan
Jan 2013 年 3 月 25 日
A can be a row-vector, such that all will operate along the 2nd dimension automatically. Therefore this is safer:
out = A(:, all(~isnan(A), 1));
HUGO LOYA
HUGO LOYA 2014 年 6 月 18 日
Why do you write 1 for columns and 2 for rows?
Jan
Jan 2014 年 7 月 13 日
@Hugo: See doc all for details. The numbers specify the dimension to operate on.
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"
verve
verve 2025 年 1 月 28 日
Adding on what @gringer45 said. The code to remove columns and/or rows with all NaN's is below.
A(:,~all(isnan(A))) % for all nan - columns
A(~all(isnan(A),2),:) % for all nan - rows

サインインしてコメントする。

その他の回答 (6 件)

Phillippe
Phillippe 2015 年 1 月 14 日

13 投票

To remove only ALL-NaN columns, do this instead:
A = A(:,~all(isnan(A)));

4 件のコメント

Jeffery Devereux
Jeffery Devereux 2017 年 6 月 20 日
Thank You!
eda
eda 2019 年 10 月 7 日
thank you so much
Parth Dev Bundela
Parth Dev Bundela 2022 年 10 月 22 日
Thanks man
osman alper altun
osman alper altun 2023 年 2 月 14 日
Thank you!

サインインしてコメントする。

Saman
Saman 2013 年 10 月 23 日
編集済み: Saman 2013 年 10 月 23 日

4 投票

Use this :
out = A(:,any(~isnan(A))); % for columns
out = A(any(~isnan(A),2),:); %for rows
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 25 日

2 投票

A(isnan(A))=[]

6 件のコメント

Jan
Jan 2013 年 3 月 25 日
This does not solve the wanted: "delete rows or cols whose elements are all NaN"
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 25 日
I am not sure what he want, from his example his rows are all 1 or all nan! In this case the answer solve his problem.
Emanuel-Petre Eni
Emanuel-Petre Eni 2016 年 4 月 4 日
Your answer will create an array. he wants to keep the matrix.
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
Andrew Sol
Andrew Sol 2024 年 11 月 14 日
You proposed too simple a solution and you were wrong.

サインインしてコメントする。

Alfaz Memon
Alfaz Memon 2018 年 8 月 20 日
編集済み: Alfaz Memon 2018 年 8 月 21 日

1 投票

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 日

0 投票

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 日

0 投票

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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

2013 年 3 月 25 日

コメント済み:

2025 年 1 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by