MATLAB: How to find the indices of NaN values in a cell array

cell array

Hi, I have a 3390 x 1 cell array containing 4 x 30 doubles. Some of these doubles contain NaN values which I would like to replace by the preceding double. How do I detect the indices of the doubles containing 'NaN' values and then replace them with the preceding 4×30 double? I have attached the cell array in question. Thanks in advance.

Best Answer

Try this:
D = load('XTrain2_all.mat');
XTrain2_all = D.XTrain2_all;
hasNaN = cellfun(@nnz,cellfun(@isnan, XTrain2_all, 'Unif',0), 'Unif',0); % Cells With ‘NaN’ Values
idx = find([hasNaN{:}]); % Their Indices
XTrain2_all(idx) = XTrain2_all(idx-1); % Replace With Previous
hasNaN = cellfun(@nnz,cellfun(@isnan, XTrain2_all, 'Unif',0), 'Unif',0); % Check

Check = find([hasNaN{:}]); % Check
.