Speeding up areas of my code

Hi,
I am lookng to speed up the following lines in my code. Both di and tmp are matrices. Below, I have posted the original line and my attempt to speed this up. However, I have actually made this line slower by trying to speed it up, so I have reverted to the original for now:
Original:
for ii=1:nn
ttt=tmp(:,1:ii)'*di(:,ii);
end
New:
for ii=1:nn
rr4=0;
for lk4=1:letg
t1 = PP_AII{lk4};
sr4=tmp(t1,1:ii)'*di(t1,ii);
rr4=rr4+sr4;
end
ttt=rr4+tmp(PP_Agamgam,1:ii)'*di(PP_Agamgam,ii);
end
Here, PP is a structure containing PP.AII and PP.Agamgam. My aim is to compute the product over the individual elements of P, then sum them all together afterwards. However, Matlab does not want me to do this, since (as mentioned) this actually slows down the code.
Does anyone have any suggestions on how to proceed?

6 Comments

It would be helpful if you could define all the parameters such that the code will execute for us. They don't have to be the full size, but maybe also give us an idea of the scale of them, as well. Sometimes that affects the strategy.
I would suggest looking at:
http://undocumentedmatlab.com/blog/matrix-processing-performance/
You seem to keep overwritting ttt in the loop. Do you mean to do this?
Sure: In general, nn will be somewhere between 200 and 800, depending on the problem. AA consists of matrices of size 2*(nn+1) square. Both tmp and di are of size nn x 2*(nn+1). Also:
PP_Agamgam=PP.Agamgam; PP_AII = PP.AII;
letg=length(PP_AII);
In general, letg is something like four, or 8.
Do you require any more information?
@Daniel I believe that I need to overwrite ttt in the loop because I need to add the tmp(PP_Agamgam,1:ii)'*di(PP_Agamgam,ii) component each time, which relies on ii.
@Daniel I had a look at your link and would like to modify the code in this way. However, my multiplication does not involve running 1:ii in both cases - I am therefore unsure how to rewrite this in order to accomodate for the dot product multiplication.

Answers (1)

Try to use (U)INT16 variables as counters:
i1 = int16;
for ii = i1:int16(nn)
rr4 = 0;
for lk4 = i1:int16(letg)
t1 = PP_AII{lk4};
sr4 = tmp(t1, i1:ii)' * di(t1, ii);
rr4 = rr4 + sr4;
end
ttt = rr4 + tmp(PP_Agamgam, i1:ii)' * di(PP_Agamgam, ii);
end

4 Comments

Hopefully, this reply does not offend. However, the first line in Matalb receives the reply of 'not enough input arguments'?!?
yes - probably a typo:
i1 = int16(1); %assuming your loops start at one
Hi,
Thanks fo the reply. I tried the above, however the result is no quicker i'm afraid (actually, a little slower)?!?
Do you have the parallel computing toolbox? There doesn't appear to be any reason why you couldn't make that outer look a parfor.

This question is closed.

Asked:

on 19 Jul 2011

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!