Remove rows or cols whose elements are all NaN

176 vues (au cours des 30 derniers jours)
Jeon
Jeon le 25 Mar 2013
Commenté : Andrew Sol le 14 Nov 2024 à 7:43
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 commentaires
Dev-iL
Dev-iL le 13 Juil 2014
Modifié(e) : Dev-iL le 13 Juil 2014
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 le 20 Sep 2015
Michal Gajewski commented
works

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 25 Mar 2013
Modifié(e) : Andrei Bobrov le 25 Mar 2013
out = A(all(~isnan(A),2),:); % for nan - rows
out = A(:,all(~isnan(A))); % for nan - columns
  5 commentaires
Céldor
Céldor le 2 Avr 2015
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 le 18 Mar 2018
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"

Connectez-vous pour commenter.

Plus de réponses (6)

Phillippe
Phillippe le 14 Jan 2015
To remove only ALL-NaN columns, do this instead:
A = A(:,~all(isnan(A)));
  4 commentaires
Parth Dev Bundela
Parth Dev Bundela le 22 Oct 2022
Thanks man
osman alper altun
osman alper altun le 14 Fév 2023
Thank you!

Connectez-vous pour commenter.


Saman
Saman le 23 Oct 2013
Modifié(e) : Saman le 23 Oct 2013
Use this :
out = A(:,any(~isnan(A))); % for columns
out = A(any(~isnan(A),2),:); %for rows

Azzi Abdelmalek
Azzi Abdelmalek le 25 Mar 2013
A(isnan(A))=[]
  6 commentaires
Walter Roberson
Walter Roberson le 28 Mai 2020
A(all(isnan(A),2),:) = []; %rows that are all nan
A(:, all(isnan(A),1)) = []; %cols that are all nan
Andrew Sol
Andrew Sol le 14 Nov 2024 à 7:43
You proposed too simple a solution and you were wrong.

Connectez-vous pour commenter.


Alfaz Memon
Alfaz Memon le 20 Août 2018
Modifié(e) : Alfaz Memon le 21 Août 2018
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 commentaires
Walter Roberson
Walter Roberson le 20 Août 2018
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 le 21 Août 2018
Modifié(e) : Alfaz Memon le 21 Août 2018
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.

Connectez-vous pour commenter.


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

Connectez-vous pour commenter.


Nike
Nike le 25 Mar 2013
Simplest is
A(isnan(A))= [];
  1 commentaire
Jan
Jan le 25 Mar 2013
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by