need to optimise my code

Hello friends,
i am writing a matlab optimisation program. Following is the segment of my code. for large value of m and n, this segment is taking longer time to execute. I want to optimise this code. Is there any body can help me in optimising this segment of code?
for s=1:m
for i=1:n
for j=1:n
for k=1:n
for l=1:n
A = A + D(k,l) * C(i,j,t) * B(i,k,s) * B(j,l,s);
end
end
end
end
end
thanks in advance...:)

1 Kommentar

Teja Muppirala
Teja Muppirala am 4 Jun. 2012
Where did you come up with this? Is this just a direct translation from C or Fortran or something? This is probably just some simple matrix operation, but you've got it all written out in loops. Just like you did here:
http://www.mathworks.com/matlabcentral/answers/38829-about-nested-for-loop

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 4 Jun. 2012

0 Stimmen

See nice Teja' solution:
A = sum(arrayfun(@(ii)sum(sum(C(:,:,ii).*(B(:,:,ii)*D*B(:,:,ii)'))),1:size(C,3)))
if t ->s

Diese Frage ist geschlossen.

Gefragt:

t g
am 4 Jun. 2012

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by