consider preallocating for speed

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

回答 (1 件)

David Hill
David Hill 2020 年 3 月 30 日

0 投票

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);

1 件のコメント

Shaghayegh Ghandali
Shaghayegh Ghandali 2020 年 3 月 30 日
omg i never though about that thank you

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2020 年 3 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by