How to Code with Preallocation, Vectorization and Only Calculating Once

Hello,
I have a code which needs speeding up and would like to get rid of my loops. How can I do this? I would like to preallocate, vectorize and only calculate it once. The code follows;
figure
for r = 0: 0.4: 3/2
r;
k=1;
for i=0:pi/2:2*pi;
theta= exp(i);
for j=0:pi/2:2*pi;
gamma= exp(j);
for deltas=i;j;k;
for t = 0
deltat= exp (t);
scatter3(r,theta,gamma,[]);
hold on
[i,j,k] = meshgrid(0: 3/2: 2*pi);
end
end
end
end
end
thank you.

Respuestas (2)

Jan
Jan el 17 de Feb. de 2013
At first look at the MLint messages:
  • deltat is not used at all, so omit the line which defines it.
  • for t=0 is more expensive than simply set "t = 0;".
  • r; wastes time only.
  • for deltas=i;j;k is not a valid FOR loop. Perhaps you want: for deltas=[i,j,k], but this is guessed only.
Currently the program is not working correctly. Therefore it is not the time to care about pre-allocation or vectorization.

1 comentario

Hi, Thank you very much. I've made all the changes you suggested. the code extracts the data fine and there seems to be no change in the data. At least my scatter3 shows the figure. So I have been mucking about a bit, but not sure if it's at all the correct path. At first glance it looks easy but as i go along, the figures seem to change which is why I'm asking others... I am sure it takes more than just simply placing all the values and vectorising them...

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 17 de Feb. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by