首页 > 解决方案 > “for”循环中“eul”函数的单矩阵结果

问题描述

我正在尝试运行一个“for”循环,通过随机选择每个循环的角度,生成 N 个“欧拉角”矩阵,然后将“欧拉角”转换为“旋转角”3x3 矩阵。我的问题是,最后我的结果似乎只有一个欧拉矩阵和一个旋转矩阵,而不是 N 个矩阵。我的代码如下,我的返回怎么可能是 4 个矩阵而不是一个?

`for s = 1 : 4;
     Aplha_x(s) = 2 * pi * (rand);

     Aplha_y(s) = 2 * pi * (rand);

     Aplha_z(s) = 2 * pi * (rand);

     eul = [Aplha_z(s) , Aplha_y(s) , Aplha_x(s)];

     rotm = eul2rotm (eul);

end `

标签: matlabfor-loopeuler-angles

解决方案


这是因为您在每次迭代时都覆盖了 rotm。

您可以使用元胞数组来存储每次迭代的矩阵,如下所示:

rotm_array = cell(4,1);

for s = 1 : 4
   Aplha_x(s) = 2 * pi * (rand);

   Aplha_y(s) = 2 * pi * (rand);

   Aplha_z(s) = 2 * pi * (rand);

   eul = [Aplha_z(s) , Aplha_y(s) , Aplha_x(s)];

   rotm = eul2rotm (eul);

   rotm_array{s} = rotm;
end

可以使用 rotm_array{s} 打印单个矩阵:

disp(rotm_array{1});

推荐阅读