Error using 'omitnan'
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2019년 6월 5일
Omitnan did not exist in your release. One of the toolboxes had nanmean()
채택된 답변
1 개 추천
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
2019년 6월 5일
편집: Adam Danz
2019년 6월 5일
Thank you very much Adam, for help,
It's correct how I inserted?
for w=1:c
for q = 1:l
if matrix(q,w)==999999
matrix(q,w) =NaN;
nanidx = isnan(matrix(:,w));
mean(matrix(~nanidx,w))
end
end
end
Grateful
Walter Roberson
2019년 6월 5일
You are not saving the result.
It looks correct (I edited your comment and formatted your code with correct indentation but I didn't make any other changes). However, there are some other issues.
1) you're not storing the value of this function anywhere: mean(matrix(~nanidx,w))
2)If you're trying to replace all values of 999999 with NaN, you can do that more efficiently outside of the loop with this: matrix(matrix==999999) = NaN
3) Is this really what you want to do? You want to take the mean of column 'w' only if matrix(q,w) is 999999? That doesn't seem right.
Guilherme Lopes de Campos
2019년 6월 5일
편집: Guilherme Lopes de Campos
2019년 6월 5일
The third option Adam,
I would to like replace the (999999) to mean of column (w);
Grateful
If you want the mean of each column, ignoring 999999 and NaN values, this is one way you can do that:
matrix(matrix==999999) = NaN;
nonNanCount = sum(~isnan(matrix),1); %number of non-NaN per column
matrixTemp = matrix; %Temporarily replace matrix
matrixTemp(isnan(matrixTemp)) = 0; %Replace NaN with 0
colMean = sum(matrixTemp,1)./nonNanCount; %mean of each column, ignoringNaN
If you want the mean of each column only if there is a 999999 within the column and ignore that value, this is one way to do that:
% PRIOR to to the 5 lines in my comment above,
colIdx = any(matrix==999999,1); % Which columns have 999999
% Then do the 5 lines in the comment above
% Then pull out the means from columns with 999999
colMean(colIdx)
Adam, thank you very much for help,
I need to replace the value where there is (999999) for a mean of
respective column,
I believe that is a little modification on code.
Grateful
Adam Danz
2019년 6월 5일
No problem; One of my comments above does that. It takes the mean of each column and ignores any 999999 values.
Guilherme Lopes de Campos
2019년 6월 10일
Hi Adam,
I tried the solution above, but it show the follow error:
Undefined function 'colMean' for input arguments of type 'logical'.
Error in depuracao_versao_2012 (line 13)
colMean(colIdx)
Could help me?
Grateful
Adam Danz
2019년 6월 10일
I've updated my answer to show a full working example with fake data.
Guilherme Lopes de Campos
2019년 6월 10일
편집: Guilherme Lopes de Campos
2019년 6월 10일
Perfectly Adam,
Works,
But I need a help more,
How I can replace each value of colMean in the respectly column?
For example, I tried the code:
matrixTemp = matriz_media .* ~isKey;
colMean = sum(matrixTemp)./rowCount;
colMean=transpose(colMean);
matrixTemp(matrixTemp==0)=colMean(1)
This code replace all zero in matrix with the mean of position 1 (colMean(n,1)),but I need the mean of column 4 replace zero in column 4, mean of column 9 replace zero in column 9 like that respectly, I could use for looping?
I tried the follow code:
for i=1:c
for j=1:l
if matrixTemp(j,i)== 0
matrixTemp(j,i)== colMean(i,1);
end
end
end
But not works
Could help me?
Very Grateful your patience.
Guilherme
Happy to help.
To address your new question, try this out
[rowIdx,colIdx] = find(matrixTemp==0);
matrixTemp(sub2ind(size(matrixTemp),rowIdx,colIdx)) = colMean(colIdx);
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
2019년 6월 10일
I've been there; glad I could help!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
