Vectorize the code by selecting only a few rows in a matrix

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)

Jan
Jan on 27 Jan 2018
Edited: Jan on 27 Jan 2018
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

Thank you for the great answer!
pdd size is: 3533x520
yes I am preallocating them.
retud=zeros(size(pdd)); %pre-allocate stdexret=zeros(size(pdd)); %pre-allocate
retud and stdexret is zero in the first win-1 columns. yes, this is wanted because the win =24 is important. although the "zero" result does not make sense for the first win-1 results for my application. but i want to make the matrix dimensions consistent for pdd, retud, stdexret.
is the vectorization on stdexret not possible without the 3d array due to the this following line ??
j-(win-1)):j
@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

Sign in to comment.

Categories

Asked:

P J
on 27 Jan 2018

Commented:

Jan
on 28 Jan 2018

Community Treasure Hunt

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

Start Hunting!