How to speed up matrix update zeros by using previous value?

17 次查看(过去 30 天)
Hello,
I am looking for the way to update my matrix zero values by using previous value. For example if c matrix is:
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
The result should look like this:
1 1 5 2 3 9 9
1 1 5 2 3 7 9
1 1 5 1 7 8 9
7 2 1 1 7 8 7
Now I use the following code:
c(c == 0) = NaN;
c = fillmissing(c,'previous');
c = fillmissing(c,'nearest',1);
However I use quite big matrixes like 10000000x150 and it takes very long to process, maybe somebody knows of a way to speed up this?

采纳的回答

Jan
Jan 2018-11-22
编辑:Jan 2018-11-22
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); % Get current column
f = (x ~= 0); % Find non-zero elements
m = cumsum(f); % Cumulative sum of logical indices
ind1 = find(f, 1); % Fill initial zeros:
if ~isempty(ind1)
m(1:ind1) = 1;
end
xf = x(f); % Vector of non-zero elements
c(:, col) = xf(m); % Replace column
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?
  6 个评论
Jan
Jan 2018-11-22
Your interjections are always welcome, because they provide deeper insights very frequently.
I'm still disappointed, that xf=x(f); y=xf(m) cannot be abbreviated. Such a "cumulated indexing" should work in one step.
Nevertheless, in a C-mex, the creation of the vectors x, f, m and xf can be avoided. @Mantas Vaitonis: If this is time-critical, install a C-compiler and ask for the posting of the small function.
Bruno Luong
Bruno Luong 2018-11-22
编辑:Bruno Luong 2018-11-22
There are shorter code using INTERP1 but both are slighly slower than your code on my benchmark. Also both can work on multicolumns so one can tune up the chunk size depending on the amount of memory available.
for col = 1:size(c, 2)
x = c(:, col);
f = (x ~= 0);
x(~f) = interp1(find(f),x(f),find(~f),'previous','extrap');
c(:, col) = x;
end
Or
for col = 1:size(c, 2)
x = c(:, col);
f = (x ~= 0);
c(:, col) = interp1(find(f),x(f),1:length(f),'previous','extrap');
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Distributed Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by