MATLAB: Isn’t this threshold loop working

dspfor loopif statementmatrix arraythreshold

I've created a simple loop to go through a 1024×240 array to set all values below the threshold of 0.04 to NaN.
for freqs = 1:F
for times = 1:T
if P(times,freqs) < 0.04
P(times,freqs) = NaN
end
end
end
I tested the same style of loop for a much smaller magic(x) array and got the exact results expected. The "P" array has plenty of 0's in it as well as (n)e-3, so seeing that nothing was happening was quite easy. Is there a simpler solution at that I'm unaware of or is there just something I'm oblivious to?

Best Answer

Try
nanMask = P < 0.04;
P(nanMask) = NaN;