如何从(30x3)矩阵生成10(3x3)矩阵并将其用于旋转矩阵

时间:2019-12-05 00:28:25

标签: matlab math multiple-columns rows rotational-matrices

我有10个b数据,这些数据是我从excel文件中读取的。我正在尝试通过替换矩阵[1 0 0;获得10(3x3)个旋转矩阵。 0 cos(a)-sin(a); 0 sin(a)cos(a)]。但是,我的代码生成(30x3)矩阵。我可以直接生成10个3x3矩阵吗,还是可以将(30x3)矩阵中的三个连续行连接在一起以构成10个3x3矩阵?

syms a b
b = xlsread('C:\Desktop\Data.xlsx');
r = subs([1 0 0 0 cos(a) -sin(a) 0 sin(a) cos(a)], b)
rx = vec2mat(r,3)

进一步发展答案以获得最终的旋转矩阵,将上述结构重复三次,如下所示

syms a b c d rx ry rz rm
b = xlsread('C:\Desktop\Data.xlsx','A1:A10');
c = xlsread('C:\Desktop\Data.xlsx','B1:B10');
d = xlsread('C:\Desktop\Data.xlsx','C1:C10');

rx = reshape(vec2mat(subs([1 0 0 0 cosd(a) -sind(a) 0 sind(a) cosd(a)], b), 3).',3,3,numel(b));
ry = reshape(vec2mat(subs([cosd(a) 0 sind(a) 0 1 0 -sind(a) 0 cosd(a)], c), 3).',3,3,numel(b));
rz = reshape(vec2mat(subs([cosd(a) -sind(a) 0 sind(a) cosd(a) 0 0 0 1], d), 3).',3,3,numel(b));

rm = rx.*ry.*rz;

对于以度数为单位的角度(34,0,100),算法给出的答案正确如下

[-cos((4*pi)/9), 0 , 0]
[0, -cos((4*pi)/9*cos((17*pi)/90), 0]
[0, 0, cos((17*pi)/90)]

代替

rm =

    0.8623         0         0
         0   -0.7317         0
         0         0   -0.8486

我正在寻找一个带有十进制值的单矩阵解决方案(不是用pi表示)。

1 个答案:

答案 0 :(得分:2)

编辑后,我什至更想知道,如果您想要的结果应该是纯数字,那么为什么您首先需要symbolic variables and functions

因此,避免使用符号:对于每个(单个)旋转矩阵以及组合的旋转矩阵,设置一个anonymous function。然后,您可以使用arrayfun为每个参数集生成组合的旋转矩阵。

以下是一些代码:

% Parameters (from file, ...)
b = [34, 20, 30];
c = [0, 21, 31];
d = [100, 22, 32];

% Anonymous functions: (Single) rotation matrices
rot_x = @(alpha) [1 0 0; 0 cos(alpha) -sin(alpha); 0 sin(alpha) cos(alpha)];
rot_y = @(beta) [cos(beta) 0 sin(beta); 0 1 0; -sin(beta) 0 cos(beta)];
rot_z = @(gamma) [cos(gamma) -sin(gamma) 0; sin(gamma) cos(gamma) 0; 0 0 1];

% Anonymous function: Combined rotation matrix
rot_m = @(alpha, beta, gamma) rot_x(alpha) .* rot_y(beta) .* rot_z(gamma);

% Calculate combined rotation matrix for all parameter sets
rot_mats = arrayfun(rot_m, b, c, d, 'UniformOutput', false)

我们得到以下输出:

rot_mats =
{
  [1,1] =
     0.86232   0.00000   0.00000
    -0.00000  -0.73174  -0.00000
    -0.00000   0.00000  -0.84857

  [1,2] =
     0.54771   0.00000   0.00000
    -0.00000  -0.40807  -0.00000
    -0.00000   0.00000  -0.22352

  [1,3] =
     0.76310  -0.00000  -0.00000
     0.00000   0.12868   0.00000
     0.00000  -0.00000   0.14110
}

如您所见,第一个完全是您的示例–但是,请注意:在您的编辑中,您使用了sindcosd,而示例中的数据表明您已经在这里使用sincos

希望有帮助!


免责声明:已在Octave 5.1.0中进行了测试,但还可以在MATLAB Online中使用。

相关问题