Matlab 在循环中乘以矩阵

Matlab Multiplying matrices in a loop

我是 matlab 的新手,我正在尝试创建一个循环,该循环将生成 6 个矩阵并在生成它们时将所有结果矩阵相乘(有点像计数器)。你可以在下面看到我的循环:

for i=1:6
    A = [   cosd(th(i)) -cosd(alpha(i))*sind(th(i)) sind(alpha(i))*sind(th(i))  L(i)*cosd(th(i)); ...
            sind(th(i)) cosd(alpha(i))*cosd(th(i))  -sind(alpha(i))*cosd(th(i)) L(i)*sind(th(i)); ...
               0   sind(alpha(i))  cosd(alpha(i))  d(i);...
               0   0   0   1];
    j = i-1;
    fprintf('DH Matrix A%d%d equals to\n', i-1, i);
    disp(A);
end   

我希望它执行以下操作

A01 = [   cos(th(1)) -cos(alpha(1))*sin(th(1)) sin(alpha(1))*sin(th(1))  L(1)*cos(th(1)); ...
          sin(th(1)) cos(alpha(1))*cos(th(1))  -sin(alpha(1))*cos(th(1)) L(1)*sin(th(1)); ...
            0   sin(alpha(1))  cos(alpha(1))  d(1);...
            0   0   0   1];

A12 = [   cos(th(2)) -cos(alpha(2))*sin(th(2)) sin(alpha(2))*sin(th(2))  L(2)*cos(th(2)); ...
          sin(th(2)) cos(alpha(2))*cos(th(2))  -sin(alpha(2))*cos(th(2)) L(2)*sin(th(2)); ...
            0   sin(alpha(2))  cos(alpha(2))  d(2);...
            0   0   0   1];

A23 = [   cos(th(3)) -cos(alpha(3))*sin(th(3)) sin(alpha(3))*sin(th(3))  L(3)*cos(th(3)); ...
          sin(th(3)) cos(alpha(3))*cos(th(3))  -sin(alpha(3))*cos(th(3)) L(3)*sin(th(3)); ...
            0   sin(alpha(3))  cos(alpha(3))  d(3);...
            0   0   0   1];

A34 = [   cos(th(4)) -cos(alpha(4))*sin(th(4)) sin(alpha(4))*sin(th(4))  L(4)*cos(th(4)); ...
          sin(th(4)) cos(alpha(4))*cos(th(4))  -sin(alpha(4))*cos(th(4)) L(4)*sin(th(4)); ...
            0   sin(alpha(4))  cos(alpha(4))  d(4);...
            0   0   0   1];

A45 = [   cos(th(5)) -cos(alpha(5))*sin(th(5)) sin(alpha(5))*sin(th(5))  L(5)*cos(th(5)); ...
          sin(th(5)) cos(alpha(5))*cos(th(5))  -sin(alpha(5))*cos(th(5)) L(5)*sin(th(5)); ...
            0   sin(alpha(5))  cos(alpha(5))  d(5);...
            0   0   0   1];

A56 = [   cos(th(6)) -cos(alpha(6))*sin(th(6)) sin(alpha(6))*sin(th(6))  L(6)*cos(th(6)); ...
          sin(th(6)) cos(alpha(6))*cos(th(6))  -sin(alpha(6))*cos(th(6)) L(6)*sin(th(6)); ...
            0   sin(alpha(6))  cos(alpha(6))  d(6);...
            0   0   0   1];

C = A01*A12*A23*A34*A45*A56;
disp(C);

如果你想使用循环创建一个单位矩阵作为初始值,并在循环中将以前的结果乘以当前结果:

C =eye(4);
for i=1:6
    C =C* [   cosd(th(i)) -cosd(alpha(i))*sind(th(i)) sind(alpha(i))*sind(th(i))  L(i)*cosd(th(i)); ...
        sind(th(i)) cosd(alpha(i))*cosd(th(i))  -sind(alpha(i))*cosd(th(i)) L(i)*sind(th(i)); ...
           0   sind(alpha(i))  cosd(alpha(i))  d(i);...
           0   0   0   1];

end 

以下是可以避免循环的矢量化版本:

Z=permute(zeros(1,6),[3 1 2]);
O=permute(ones(1,6),[3 1 2]);
I = permute(1:6,[3 1 2]);
A0 = [   cosd(th(I)) -cosd(alpha(I)).*sind(th(I)) sind(alpha(I)).*sind(th(I))  L(I).*cosd(th(I)); ...
        sind(th(I)) cosd(alpha(I)).*cosd(th(I))  -sind(alpha(I)).*cosd(th(I)) L(I).*sind(th(I)); ...
           Z   sind(alpha(I))  cosd(alpha(I))  d(I);...
           Z   Z   Z   O];
B = mat2cell(A0,4,4,ones(1,6));
C = mtimes(B{:})