Info

This question is closed. Reopen it to edit or answer.

need to optimise my code

1 view (last 30 days)
t g
t g on 4 Jun 2012
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comment
Teja Muppirala
Teja Muppirala on 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

Answers (1)

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

Community Treasure Hunt

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

Start Hunting!