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.

답변 (2개)

Jan
Jan 2013년 2월 17일

0 개 추천

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

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...

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

제품

질문:

2013년 2월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by