MATLAB: Find and replace values in a cell array containing 3-D matrixes with numeric values

cell arrayfind and replacenumeric values

Hi, I have a cell array with 6 cells, each cell containing a 3-D matrix of 640x480x30. I'm trying to find all zeros (0) in the cell array and replace them with NaNs.
In the general case, I have a cell array (MyCellArray) with -K- cells, each cell containing an arbitrary vector or matrix in an arbitrary size, with numeric values. I'm trying to find all places where MyCellArray contains the value of X (some number) and replace it with Y (some number).
Is there a way to do it without a for loop? Something like (that of course can't work) MyCellArray{MyCellArray==0}=NaN
Thanks!

Best Answer

Another possible solution:

YourCellArray = cellfun(@replaceZeroWithNan,YourCellArray,'UniformOutput',false);
function D = replaceZeroWithNan(D)
idx = D == 0;
D(idx) = nan;
end