I want to use a matrix and a function to then create another function

1 view (last 30 days)
I currently have a matrix that has known values that I then want to use to create a second matrix using a function loop. The matrix within the function loop has four variables that coincide with the variables from the above matrix DH. How do I use each row of the DH matrix to be a value to then insert into the Ai matrix? (Colum 1 = theta, Column 2 = d, etc.) I need this to then output 6 different matrices A1,A2,etc.
clear
clc
close all
theta1 = -50;
theta2 = 30;
theta3 = 15;
theta4 = -75;
theta5 = -45;
theta6 = -60;
DH = [theta1, 486.5, 150, -90;...
theta2+90, 0, -700, 0;...
theta3, 0, 0, 90;...
theta4, 600, 0, -90;...
theta5, 0, 0, 90;...
theta6, 65, 0, 0];
function Ai = getHomogeneousTransform(theta, d, a, alpha)
Ai = [cos(theata), -sin(theta)*cos(theta), sin(theta)*sin(alpha),a*cos(theta);...
sin(theta),cos(theta)*cos(alpha),-cos(theta)*sin(alpha),a*sin(theta);...
0, sin(alpha), cos(alpha), d;...
0,0,0,1];
end
  1 Comment
Stephen23
Stephen23 on 29 Nov 2022
"How do I use each row of the DH matrix to be a value to then insert into the Ai matrix?"
Use a loop and indexing.
"I need this to then output 6 different matrices A1,A2,etc."
Numbering variable names like that forces you into writing slow, complex, inefficient, buggy code. Best avoided.
The simple and very efficient approach is to use indexing. You should use indexing.

Sign in to comment.

Answers (1)

Torsten
Torsten on 28 Nov 2022
theta1 = -50;
theta2 = 30;
theta3 = 15;
theta4 = -75;
theta5 = -45;
theta6 = -60;
DH = [theta1, 486.5, 150, -90;...
theta2+90, 0, -700, 0;...
theta3, 0, 0, 90;...
theta4, 600, 0, -90;...
theta5, 0, 0, 90;...
theta6, 65, 0, 0];
A = cell(size(DH,1),1);
for i = 1:size(DH,1)
A{i} = getHomogeneousTransform(DH(i,1)*pi/180, DH(i,2), DH(i,3), DH(i,4)*pi/180);
A{i}
end
ans = 4×4
0.6428 0.4924 0.7660 96.4181 -0.7660 0.0000 0.6428 -114.9067 0 -1.0000 0.0000 486.5000 0 0 0 1.0000
ans = 4×4
-0.5000 0.4330 0 350.0000 0.8660 -0.5000 0 -606.2178 0 0 1.0000 0 0 0 0 1.0000
ans = 4×4
0.9659 -0.2500 0.2588 0 0.2588 0.0000 -0.9659 0 0 1.0000 0.0000 0 0 0 0 1.0000
ans = 4×4
0.2588 0.2500 0.9659 0 -0.9659 0.0000 0.2588 0 0 -1.0000 0.0000 600.0000 0 0 0 1.0000
ans = 4×4
0.7071 0.5000 -0.7071 0 -0.7071 0.0000 -0.7071 0 0 1.0000 0.0000 0 0 0 0 1.0000
ans = 4×4
0.5000 0.4330 0 0 -0.8660 0.5000 0 0 0 0 1.0000 65.0000 0 0 0 1.0000
function Ai = getHomogeneousTransform(theta, d, a, alpha)
Ai = [cos(theta),-sin(theta)*cos(theta),sin(theta)*sin(alpha),a*cos(theta);...
sin(theta),cos(theta)*cos(alpha),-cos(theta)*sin(alpha),a*sin(theta);...
0,sin(alpha),cos(alpha),d;...
0,0,0,1];
end

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!