Remove rows or cols whose elements are all NaN
Mostra commenti meno recenti
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 Commenti
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
il 20 Set 2015
Michal Gajewski commented
works
Risposta accettata
Più risposte (6)
Phillippe
il 14 Gen 2015
13 voti
To remove only ALL-NaN columns, do this instead:
A = A(:,~all(isnan(A)));
4 Commenti
Jeffery Devereux
il 20 Giu 2017
Thank You!
eda
il 7 Ott 2019
thank you so much
Parth Dev Bundela
il 22 Ott 2022
Thanks man
osman alper altun
il 14 Feb 2023
Thank you!
Use this :
out = A(:,any(~isnan(A))); % for columns
out = A(any(~isnan(A),2),:); %for rows
Azzi Abdelmalek
il 25 Mar 2013
A(isnan(A))=[]
6 Commenti
Jan
il 25 Mar 2013
This does not solve the wanted: "delete rows or cols whose elements are all NaN"
Azzi Abdelmalek
il 25 Mar 2013
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
il 4 Apr 2016
Your answer will create an array. he wants to keep the matrix.
Serafeim Zacharopoulos
il 27 Mag 2020
To delete rows with all NaN's, maybe try this:
A = A(find(sum(~isnan(A)'))',:)
Walter Roberson
il 28 Mag 2020
A(all(isnan(A),2),:) = []; %rows that are all nan
A(:, all(isnan(A),1)) = []; %cols that are all nan
Andrew Sol
il 14 Nov 2024
You proposed too simple a solution and you were wrong.
Alfaz Memon
il 20 Ago 2018
Modificato: Alfaz Memon
il 21 Ago 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 Commenti
Walter Roberson
il 20 Ago 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
il 21 Ago 2018
Modificato: Alfaz Memon
il 21 Ago 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.
Ilham Hardy
il 25 Mar 2013
Haven't tried this, but it should works:
A(isnan(A))=[];
1 Commento
Jan
il 25 Mar 2013
This does not solve the wanted: "delete rows or cols whose elements are all NaN"
Categorie
Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!