Nested Loops issue

2 views (last 30 days)
James
James on 18 Jul 2011
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
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)?

Sign in to comment.

Answers (2)

Jan
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
James on 18 Jul 2011
Thanks for the extra reply. However, the 'improved' output does not correspond to the same dimensions as the original. (t2' * AAii(t1, t1) * t2) is just a scalar, however (t2' * t2) * AAii(t1, t1) will not be a scalar (in fact, the matrix dimensions do not actually agree).
Jan
Jan on 18 Jul 2011
This is the fact, if t1 is not a scalar, in opposite to my wrong assumptions.

Sign in to comment.


Andrei Bobrov
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
Jan
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
Andrei Bobrov on 19 Jul 2011
Dear Jan! I completely agree with your approach.

Sign in to comment.

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!