Error using 'omitnan'

조회 수: 18 (최근 30일)
Guilherme Lopes de Campos
Guilherme Lopes de Campos 2019년 6월 5일
댓글: Adam Danz 2019년 6월 10일
Hi MATLAB community,
I wrote a code for identify 999999 to substitute to NaN, my purpose is replace NaN to mean of column data,
I tried follow code:
[l,c]=size(matrix) % size (324,25)
for w=1:c
for q = 1:l
if matrix(q,w)==999999
matrix(q,w) =NaN;
matrix(isnan(matrix))= mean(matrix(:,w),'omitnan'); % in the case NaN is present in column 21, it will substitute the element for mean of all element in column 21
end
end
end
Could help me?
Grateful
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 6월 5일
Omitnan did not exist in your release. One of the toolboxes had nanmean()

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 6월 5일
편집: Adam Danz 2019년 6월 10일
Check out nanmean() in the stats & machine learning toolbox.
An alternative that doesn't require any toolboxes:
nanidx = isnan(matrix(:,w));
mean(matrix(~nanidx,w)
Update following comments below
To compute the mean of each column of a matrix while ignoring a key value (999999),
% Create fake data
key = 999999;
matrix = magic(10);
matrix(randi(100,1,10)) = key;
% Identify the columns that contain at least one 999999
isKey = matrix == key;
colIdx = any(isKey,1);
% Count the number of rows per column that are not 999999
rowCount = sum(~isKey);
% Temporarily replace 999999 with 0 and calculate the column means
matrixTemp = matrix .* ~isKey;
colMean = sum(matrixTemp)./rowCount;
% The 2nd block above can be reduced to
% isKey = matrix == key;
% colMean = sum(matrix .* ~isKey)./ sum(~isKey);
  댓글 수: 14
Guilherme Lopes de Campos
Guilherme Lopes de Campos 2019년 6월 10일
Thank so much Adam,
I don't have words to show my thanks,
I am very grateful,
Guilherme
Adam Danz
Adam Danz 2019년 6월 10일
I've been there; glad I could help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resampling Techniques에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by