nested for loop take ages to run, any suggestions ?

I have a code which contains 3 for loops as follows
dt=0.01
for x=1:15
for y-2000:2000
t=sqrt(x^2+y^2)/1000
for i=1:100000 % calaculate over period of time
a= x*(y+c/t)
b= cos(x/pi)
c=@function(x,y)
a,b and c are arbitrary but i meant that i have some variables which are calculated using x,y and t
q(x,y,t)=scalar
s(x,y,t)=scalar
end
store x values in a vector
store y values in a vector
store q values in a vector
store s values in a vector
t=t+dt;
end
end
this code would take weeks to run and I haven't been able to find a way to run it faster.
the third loop is the one taking time.
I dont't know if parallel computing could help or not?
any suggestions or advice ?

6 Comments

for i=1000000
only executes once. Did you mean
for i=1:1000000
Unfortunately the outline you gave is too vague for us to be able to advise you about vectorization or parallel opportunities.
You are calculating t(x,y) and using scalar-x, scalar-y, and entire-array-t as subscripts. Did you pre-allocate so you do not end up growing the arrays dynamically ? Is i being used as an index or is it an iteration counter ?
y starts from 0 and 0 cannot be used as a subscript.
Within any x, y pair, does the results for any given i depend upon calculations done for i-1 ?
Thank you for your reply and for editing
in few words:
in the first two loops I need to calculate the time t for each x and y
i,e for (1,-2000) i'll get some t
then in the third loop i want to use that points(1,-2000,t) to calculate the velocity over some time for specifice step(dt=0.01)
then to take (1,-3999) get t then do the third loop again and so on.
I've editied the question thank to you.
I'm not sure if I've explaind it or made more mess :)
hope this explains what i want to do
If you were to use
xvals = 1:15;
yvals = -2000:2000;
[x, y] = ndgrid(xvals, yvals);
then you could probably calculate the time evolution in vectorized form for significant portions of the calculations. For some of the calculations you might need to iterate over the matrix.
Thank you Walter I really appreciate your help.
do you mean that there is no way other that the for loop ? for the third loop ?
You are evolving in time. Unless there are analytic solutions that can directly give you the value at any particular time without doing the previous steps, then you need to have gone through the previous steps. You probably cannot eliminate the time loop.
In some cases, the contribution at a particular time can be calculated without knowing what has happened in the past, but the net value at a time is the sum of the previous contributions. In cases like that you can sometimes calculate the contributions individually in vectorized form and then cumsum() them along the time dimension.
Depending on exactly what functions you are using, what you might be able to do is eliminate the first and second loop, leaving only the time loop, using vectorized operations to calculate for all of the x, y locations simultaneously.
how can I create a 3D matrix as for each xi,yj I get an array for time ?
like i take t and expand it to be 3D matrix so I get rid of the for loop and maybe use parallel computation to run it faster.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2017a

Asked:

on 18 May 2019

Commented:

on 21 May 2019

Community Treasure Hunt

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

Start Hunting!