Nested Loops issue
Show older comments
Hi, Please bear with me, as I am still learning with Matlab. I have a problem with efficiency of a nested loop in my Matlab code (I have copied and pasted this below).
for ii=1:nn
for jr1=1:length(PP.AII)
sr1=(d(PP.AII{jr1})'*AA{ii}(PP.AII{jr1},PP.AII{jr1})*d(PP.AII{jr1}));
dr1=dr1+sr1;
end
end
When the number of elements of PP.AII increases, this loop becomes extremely slow. Ideally, I would like to write something like:
for ii=1:nn
sr1=(d(PP.AII)'.*AA{ii}(PP.AII,PP.AII).*d(PP.AII));
sr1=sum(sr1);
end
However, Matlab will not allow this due to the fact that PP.AII consists of cell elements (for example, [90x1 double] [100x1 double] [90x1 double] [100x1 double]). I can find a way around this using the cell2mat and num2cell commands, however I feel that this is still inefficient due to introducing another nested loop, which seems to defeat the object.
Is there a way in which I can employ the dot command in order to speed up the above process? Failing this, does anyone have any other suggestions on how to speed up this area of code?
Any help is appreciated.
1 Comment
Oleg Komarov
on 18 Jul 2011
How big is d (m by n size) and how big is PP (in terms of megabytes)? What system do you use (32/64 bit)?
Answers (2)
Jan
on 18 Jul 2011
At first I'd omit all repeated calculations output the loop(s) and use temporary variables:
PP_AII = PP.AII;
n = length(PP_AII);
for ii = 1:nn
AAii = AA{ii};
for jr1 = 1:n
t1 = PP_AAI{jr1};
t2 = d(t1);
dr1 = dr1 + (t2' * AAii(t1, t1) * t2);
% [EDITED] If t2 is a column vector, this is much faster:
% dr1 = dr1 + (t2' * t2) * AAii(t1, t1);
end
end
Please compare the speed with your original approach and the CELL2MAT method.
CELL2MAT is not efficient for larger cells (thousands of elements), because it seems to let the output grow dynamically instead of a clean pre-allocation. You can use FEX: Cell2Vec, but some reshaping / permutating might be necessary to create the wanted output.
6 Comments
James
on 18 Jul 2011
James
on 18 Jul 2011
Jan
on 18 Jul 2011
@James: I hoped that the speedup is higher than 10%. But 10 seconds seems to be surprisingly slow for this piece of code. What is "d()" and how large is n? Is it possible that the main time is spend in the function(?) d()? Please use the PROFILEr or TIC/TOC to find the real bottlenecks of the program.
If I assume that t2 is a [N x 1] vector, the processing is much faster if you reorder the elements:
dr1 = dr1 + (t2' * AAii(t1, t1) * t2);
==>
dr1 = dr1 + (t2' * t2) * AAii(t1, t1);
In the later case MATLAB uses an efficient BLAS method and the external multiplication is much cheaper.
James
on 18 Jul 2011
James
on 18 Jul 2011
Jan
on 18 Jul 2011
This is the fact, if t1 is not a scalar, in opposite to my wrong assumptions.
Andrei Bobrov
on 18 Jul 2011
variant 1
c = PP.AII;
sr1 = sum(cell2mat(cellfun(@(x)sum(cell2mat(arrayfun(@(i1)d(c{i1})'*x(c{i1},c{i1})*d(c{i1}),1:length(c),'un',0))),AA,'un',0)));
variant 2
nn = length(AA);
mm = length(PP.AII);
sr = zeros(nn,mm);
for ii=1:nn
sr(ii,:) =arrayfun(@(i1)d(PP.AII{i1})'*AA{ii}(PP.AII{i1},PP.AII{i1})*d(PP.AII{i1}),1:mm);
end
sr1 = sum(sr(:));
7 Comments
James
on 18 Jul 2011
James
on 18 Jul 2011
James
on 18 Jul 2011
Jan
on 18 Jul 2011
@James: Please describe "it will not work" with any details. Solving problems is dramatically more efficient, if the problems are known already.
James
on 18 Jul 2011
Jan
on 18 Jul 2011
@Andrei: Your method evaluates "d(PP.AII{i1})" twice and "PP.AII{i1}" 4 times. Using a temp variable would be more efficient.
Andrei Bobrov
on 19 Jul 2011
Dear Jan! I completely agree with your approach.
Categories
Find more on Loops and Conditional Statements 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!