MATLAB: Convert nans to 0

anzeroes

Hi I have a large matrix and want to convert all nans to zeros. Any help?

Best Answer

% Sample input with some NaNs
A = rand(10);
A(randi([1 100],[20,1])) = NaN;
% Detect and replace NaNs
idx = isnan(A);
A(idx) = 0;