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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by