Start with:
c = fillmissing(c, 'previous', 'EndValues', 'nearest');
Another idea is to process the matrix column-wise:
c = [0 1 5 2 3 9 0
1 0 5 0 0 7 0
0 0 0 1 7 8 9
7 2 1 0 0 0 7];
for col = 1:size(c, 2)
x = c(:, col);
f = (x ~= 0);
m = cumsum(f);
ind1 = find(f, 1);
if ~isempty(ind1)
m(1:ind1) = 1;
end
xf = x(f);
c(:, col) = xf(m);
end
This avoids the useless replacing of 0 to NaN and the creation of the huge temporary matrices.
This code fails, if the column contains zeros only.
A C-mex file could avoid the creation of f, m and xf. Do you have a C-compiler?
Best Answer