consider preallocating for speed

1 view (last 30 days)
Shaghayegh Ghandali
Shaghayegh Ghandali on 30 Mar 2020
so i was wondering is there any way for fixing it i just don't want to use this :
theta_2= zeros(i,1);
th_2 =zeros(i,1);
A_X =zeros(i,1);
A_Y=zeros(i,1);
for each and indivual outputs before my for loop
this is my script
for i=1:360
theta_2(1)= 0; % initial angle for link 2
theta_2(i)=i;
th_2(i)= theta_2(i)*pi/180; % theta 2 in radians
% using equation from book to calulate the postion of the links
% position of link 2 (AO2)
A_X(i) = R2*cos(th_2(i)); % position x of link 2
A_Y(i) = R2*sin(th_2(i)); % position y of link 2
end
i have so much more to put in this for loop and i don't want to get that warrning

Answers (1)

David Hill
David Hill on 30 Mar 2020
No for-loop needed. No preallocation needed here.
theta_2=1:360;
th_2=theta_2*pi/180;%you could use cosd() and not need to convert
A_X=R2*cos(th_2);
A_y=R2*sin(th_2);

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!