Nested Loops issue

조회 수: 1 (최근 30일)
James
James 2011년 7월 18일
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
Oleg Komarov
Oleg Komarov 2011년 7월 18일
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)?

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2011년 7월 18일
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
James
James 2011년 7월 18일
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 2011년 7월 18일
This is the fact, if t1 is not a scalar, in opposite to my wrong assumptions.

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2011년 7월 18일
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
Jan
Jan 2011년 7월 18일
@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 2011년 7월 19일
Dear Jan! I completely agree with your approach.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by