Hi Jules,
To combine multiple matrices while replacing 'NaN' values with corresponding elements from other matrices, you can indeed use a loop. However, the issue in your current approach is that you're not updating the 'xn' (NaN indices) after each iteration, which causes only the 'NaN' values from the first matrix to be replaced.
Here's how you can modify your code to achieve cumulative merging:
% Initialize 'or' with the first matrix
or = Cx(1).Z;
% Loop through each subsequent matrix
for i = 2:numel(Cx)
nr = Cx(i).Z;
% Find NaN indices in the current 'or' matrix
xn = isnan(or);
% Replace NaN values in 'or' with values from the current matrix 'nr'
or(xn) = nr(xn);
end
% 'or' now contains the combined matrix with NaNs replaced