Vectorize the code by selecting only a few rows in a matrix
Show older comments
Hi, pdd is an m*n matrix. as the size of pdd matrix is increasing my loop is getting slower and slower. How can I vectorize this code below. basically, I am making calculations by using a subset of rows from the original matrix.
win=24;
sec=size(pdd,2);
for j= win :size(pdd,1)
retud(j,:)=(pdd(j,:)-pdd((j-(win-1)),:))./pdd((j-(win-1)),:);
stdexret(j,:)=std(bsxfun(@minus, pdd((j-(win-1)):j,:), pdd((j-(win-1)):j,sec)),1);
end
Answers (1)
Start with a pre-allocation: Do not let arrays grow iteratively. Even the editor warns you to avoid this. If you do pre-allocate the output already, it is a bad idea not to include the lines in the posted code, because this confused the readers.
[s1, sec] = size(pdd);
win = 24;
retud = zeros(s1, sec); % Pre-allocate!!!
stdexret = zeros(s1, sec); % Pre-allocate!!!
for j = win:s1
retud(j, :) = pdd(j,:) ./ pdd(j - win + 1, :) - 1; % Cheaper
stdexret(j, :) = std(bsxfun(@minus, pdd((j - win + 1):j, :), ...
pdd((j - win + 1):j, sec)), 1);
end
Processing arrays rowwise is more expensive than the columnwise order. So try to transpose the inputs at first. Then you have a fair loop method for a test, if the vectorized code is faster at all. For some guessed input data:
pdd = rand(1000, 1000)
the runtime decreased from 4.95 (original version) to 0.39 seconds (with pre-allocation).
For the speed of the code, the absolute sizes matter. So please mention them.
Your retud and stdexret is zero in the first win-1 columns. Is this wanted?
The vectorization of producing retud is easy:
[s1, sec] = size(pdd);
retud = zeros(s1, sec);
retud(win:s1, :) = pdd(win:s1, :) ./ pdd(1:s1 - win + 1, :) - 1;
Compared with the (following) columnwise processing, this is slightly slower. For computing stdexret a large temporary 3D array would be required, and I assume that this is much slower than an efficient loop.
Let's try to process the data columnwise:
pddt = pdd.';
[sec, s1] = size(pddt);
win = 24;
retud = zeros(sec, s1); % Pre-allocate!!!
stdexret = zeros(sec, s1); % Pre-allocate!!!
tmp = pddt(sec, :);
for j = win:s1
retud(:, j) = pddt(:, j) ./ pddt(:, j - win + 1) - 1;
stdexret(:, j) = std(bsxfun(@minus, pddt(:, (j - win + 1):j), ...
tmp((j - win + 1):j)), 1, 2);
end
retud = retud.';
stdexret = stdexret.';
0.28 seconds. Fine. I do not see a great potential for vectorizing here.
3 Comments
P J
on 27 Jan 2018
Jan
on 28 Jan 2018
@P J: Thanks for the clarifications. Please include important details like a pre-allocation in the question. This would have saved some time for answering.
This is the fastest version I've found for the inputs of size 3533x520:
[s1, sec] = size(pdd);
win = 24;
retud(win:s1, :) = pdd(win:s1, :) ./ pdd(1:s1 - win + 1, :) - 1;
pddt = pdd.';
stdexret = zeros(sec, s1); % Pre-allocate!!!
tmp2 = pddt - pddt(sec, :);
for j = win:s1
stdexret(:, j) = std(tmp2(:, (j - win + 1):j), 1, 2);
end
stdexret = stdexret.';
You can be 10% faster if you replace std() by:
function S = mystd(X)
s2 = size(X, 2);
M = sum(X, 2) / s2;
XC = bsxfun(@minus, X, M);
S = DNorm2(XC, 2) ./ sqrt(s2);
end
using https://www.mathworks.com/matlabcentral/fileexchange/29035-dnorm2. (All timings measured under R2016b)
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!