MATLAB: Unexpected NaN results when replacing elements in a large matrix using logical indexing
I'm getting frustrated with I'm sure I'm missing something obvious here, but I'm working with an scenario in MATLAB where I expect to replace certain values in a large matrix with NaN, but I'm getting unexpected results. I'm using MATLAB R2023a and have a matrix of size 1000x1000. The code below is meant to replace all elements below a threshold with NaN: ```matlab threshold = 0.5; A = rand(1000); % Create a random 1000x1000 matrix A(A < threshold) = NaN; ``` While this seems straightforward, I noticed that after running this code, the matrix A still contains some values that are below the threshold, which should have been replaced. When I check with `any(A(:) < threshold)`, it returns `true`. I suspect it has something to do with how MATLAB handles NaN values in logical indexing, but I'm not sure why the replacements aren't occurring as expected. I've tried using `isnan(A)` to verify the replacements, but that also indicated that the NaN values were not set correctly. Is there a known scenario or a best practice for replacing elements in this manner? Any insights or suggestions would be greatly appreciated! I'm working on a CLI tool that needs to handle this.