My for loop gives an error

1 view (last 30 days)
Muhammad Waseem
Muhammad Waseem on 27 Mar 2019
Commented: Luna on 28 Mar 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))

Accepted Answer

Luna
Luna on 27 Mar 2019
Edited: Luna on 27 Mar 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 Comments
Muhammad Waseem
Muhammad Waseem on 27 Mar 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 on 28 Mar 2019
Your welcome :)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!