replacing Nan with 0
조회 수: 29 (최근 30일)
이전 댓글 표시
I have an excel file that I need to replace nan with zeros
I am using this piece of code and am getting error : Array indices must be positive integers or logical values
Error in "isnan(auPoints(r,c)) = 0;
I am not sure how to fix it or what I am doing wrong
for r = 4:1:22
for c = 2:1:29
if isnan(auPoints(r,c))
isnan(auPoints(r,c))
end
end
end
댓글 수: 0
답변 (3개)
Star Strider
2020년 7월 25일
Try this:
auPoints - fillmissing(auPoints, 'constant',0);
It might be advisable to save the output to a different variable, in order to preserve the original ‘auPoints’ matrix.
댓글 수: 0
madhan ravi
2020년 7월 25일
편집: madhan ravi
2020년 7월 25일
auPoints(isnan(auPoints(r, c))) = 0
댓글 수: 2
madhan ravi
2020년 7월 25일
편집: madhan ravi
2020년 7월 26일
Well it’s assumed that c was within the bounds.
Try this: (OBSCURE)
auPoints = subsasgn(auPoints, substruct('()', {isnan(auPoints)}), 0)
jonas
2020년 7월 25일
The line does not work because the left part just returns a logical.
isnan(auPoints(r,c)) = 0
It is similar to writing
isnan(NaN) = 0
...which makes no sense but returns the same error message
For this type of basic operation it is better to make use of logical indexing. For example, if you want to replace all your NaNs with zeros, you can write the following
mask = isnan(auPoints);
auPoints(mask) = 0;
The first line returns a logical array (1's and 0's) with the same size as the original array. This array is then used to index the original array. Faster and easier than loops.
댓글 수: 4
Houss Leng
2022년 8월 10일
I have this error Undefined function 'isnan' for input arguments of type 'table'.
Walter Roberson
2022년 8월 10일
https://www.mathworks.com/help/matlab/ref/fillmissing.html like Star Strider suggests
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!