My for loop gives an error

1 回表示 (過去 30 日間)
Muhammad Waseem
Muhammad Waseem 2019 年 3 月 27 日
コメント済み: Luna 2019 年 3 月 28 日
alpha=sym('alpha',[1 6])
theta=sym('theta',[1 6])
d=sym('d',[1 6])
a=sym('a',[1 6])
A=sym('A',[1 6])
for S=[1 2 3 4 5 6]
A(S)=[cos(theta(S)),-sin(theta(S))*cos(alpha(S)),sin(theta(S))*sin(alpha(S)),a(S)*cos(theta(S))
sin(theta(S)),cos(theta(S))*cos(alpha(S)),-cos(theta(S))*sin(alpha(S)),a(S)*sin(theta(S))
0,sin(alpha(S)),cos(alpha(S)),d(S)
0,0,0,1]
end
My answer is as follows
>> NEW
alpha =
[ alpha1, alpha2, alpha3, alpha4, alpha5, alpha6]
theta =
[ theta1, theta2, theta3, theta4, theta5, theta6]
d =
[ d1, d2, d3, d4, d5, d6]
a =
[ a1, a2, a3, a4, a5, a6]
A =
[ A1, A2, A3, A4, A5, A6]
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in sym/privsubsasgn (line 1067)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 904)
C = privsubsasgn(L,R,inds{:});
Error in NEW (line 7)
A(S)=[cos(theta(S)),-sin(theta(S))*cos(alpha(S)),sin(theta(S))*sin(alpha(S)),a(S)*cos(theta(S))

採用された回答

Luna
Luna 2019 年 3 月 27 日
編集済み: Luna 2019 年 3 月 27 日
You can put your symbolic 4x4 matrices into a cell array.
Also your for loop is wrongly defined like for S = [1 2 3...]. You should define for S from 1 to 6.
alpha=sym('alpha',[1 6])
theta=sym('theta',[1 6])
d=sym('d',[1 6])
a=sym('a',[1 6])
% A=sym('A',[1 6])
A_new = cell(1,6); % preallocation of cell array
for S = 1:6
A_new{S} = [cos(theta(S)),-sin(theta(S))*cos(alpha(S)),sin(theta(S))*sin(alpha(S)),a(S)*cos(theta(S))
sin(theta(S)),cos(theta(S))*cos(alpha(S)),-cos(theta(S))*sin(alpha(S)),a(S)*sin(theta(S))
0,sin(alpha(S)),cos(alpha(S)),d(S)
0,0,0,1]
end
So whenever you want to reach them individually you can type:
A_new{1}
it gives the result for 1st 4x4 matrix as follows:
>> A_new{1}
ans =
[ cos(theta1), -cos(alpha1)*sin(theta1), sin(alpha1)*sin(theta1), a1*cos(theta1)]
[ sin(theta1), cos(alpha1)*cos(theta1), -sin(alpha1)*cos(theta1), a1*sin(theta1)]
[ 0, sin(alpha1), cos(alpha1), d1]
[ 0, 0, 0, 1]
  2 件のコメント
Muhammad Waseem
Muhammad Waseem 2019 年 3 月 27 日
Thank you for the help, i didn't know how i would've pre-allocate a cell array i'll keep that in mind. During this and the next code
Luna
Luna 2019 年 3 月 28 日
Your welcome :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by