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

Antworten (3)

Star Strider
Star Strider am 25 Jul. 2020

1 Stimme

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.
See the documentation for fillmissing (R2016b and later versions) for details.
madhan ravi
madhan ravi am 25 Jul. 2020
Bearbeitet: madhan ravi am 25 Jul. 2020

0 Stimmen

auPoints(isnan(auPoints(r, c))) = 0

2 Kommentare

Caroline Prekker
Caroline Prekker am 25 Jul. 2020
I'm not sure where I would put that. It is telling me that c exceeds array bounds
madhan ravi
madhan ravi am 25 Jul. 2020
Bearbeitet: madhan ravi am 26 Jul. 2020
Well it’s assumed that c was within the bounds.
Try this: (OBSCURE)
auPoints = subsasgn(auPoints, substruct('()', {isnan(auPoints)}), 0)

Melden Sie sich an, um zu kommentieren.

jonas
jonas am 25 Jul. 2020

0 Stimmen

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.
More about logical indexing and indexing in general here.

4 Kommentare

Caroline Prekker
Caroline Prekker am 25 Jul. 2020
this gave me an error saying "unrecognized function or variable 'mask'"
jonas
jonas am 25 Jul. 2020
Bearbeitet: jonas am 25 Jul. 2020
I don't see how that is possible. The variable mask is literally defined in the first out of the two lines.
Still, you could try the following:
auPoints(isnan(auPoints)) = 0
Note that this is exactly the same, just reduced to one line
Houss Leng
Houss Leng am 10 Aug. 2022
I have this error Undefined function 'isnan' for input arguments of type 'table'.
Walter Roberson
Walter Roberson am 10 Aug. 2022
https://www.mathworks.com/help/matlab/ref/fillmissing.html like Star Strider suggests

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 25 Jul. 2020

Kommentiert:

am 10 Aug. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by