My for loop gives an error

1 Ansicht (letzte 30 Tage)
Muhammad Waseem
Muhammad Waseem am 27 Mär. 2019
Kommentiert: Luna am 28 Mär. 2019
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))

Akzeptierte Antwort

Luna
Luna am 27 Mär. 2019
Bearbeitet: Luna am 27 Mär. 2019
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 Kommentare
Muhammad Waseem
Muhammad Waseem am 27 Mär. 2019
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 am 28 Mär. 2019
Your welcome :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by