Remove NaN from a matrix

8 vues (au cours des 30 derniers jours)
Damith
Damith le 3 Juin 2014
Commenté : Damith le 4 Juin 2014
Hi,
I have matrix B = (366 x 39) and has NaN values in every column. How can I remove NaN and obtain the mean of every column. The resulting matrix C should be a 1 x 39 matrix.
Thanks in advance.

Réponses (4)

George Papazafeiropoulos
George Papazafeiropoulos le 3 Juin 2014
Modifié(e) : George Papazafeiropoulos le 3 Juin 2014
ind=sum(~isnan(B));
B(isnan(B))=0;
mean=sum(B)./ind

James Tursa
James Tursa le 3 Juin 2014
If you have the appropriate toolbox, you can use the function: nanmean
  1 commentaire
Damith
Damith le 4 Juin 2014
Thanks

Connectez-vous pour commenter.


dpb
dpb le 3 Juin 2014
If have the Stats Toolbox, then just nanmean
If don't, it's more of a pain; unless the NaN are consistent in number per column you'll end up w/ a "jagged" array which isn't allowed in an array. Hence you'll need to accumulate by column--the "deadahead" solution is a loop and for small matrices of this order it'll undoubtedly be "fast enough"...
nc=size(B,2); % number columns in B
m=zeros(1,nc); % preallocate for the means vector
for i=1:nc
m(i)=mean(isfinite(B(:,i))); % compute mean of values less NaN's and Inf's
end
You can use accumarray for this w/o the loop...seed doc's for details
  1 commentaire
Damith
Damith le 4 Juin 2014
Thanks.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 3 Juin 2014
You might want to take a look at John D'Errico's inpaint_nans: http://www.mathworks.com/matlabcentral/fileexchange/4551-inpaint-nans

Catégories

En savoir plus sur NaNs 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