根据一个矩阵中包含的数据创建一系列矩阵

时间:2014-11-29 18:55:16

标签: matlab matrix extraction

我正在尝试根据预定义矩阵中包含的数据创建一系列矩阵。

我写了一些代码,要求用户指定4列矩阵的行数。

然后用户将为他们指定的每一行输入4个元素,从而创建一个k×4矩阵。

即如果用户选择3行,则它们将为行1输入x1,x2,x3,x4,然后为行2输入y1,y2,y3,y3,然后为第3行输入z1,z2,z3,z4,因此创建的矩阵变为: J = [x1,x2,x3,x4;y1,y2,y3,y3;z1,z2,z3,z4]

我的问题是,我需要从矩阵J中获取数据以创建一系列新矩阵。新矩阵的数量取决于矩阵J所包含的行数,即如果矩阵J有5行,那么我需要从矩阵J中包含的数据创建5个新矩阵,例如T1,T2,T3,T4, T5

新创建的矩阵(T)需要来自J矩阵的元素,如下所示:

T1 = [cos(J(1,1)), -cos(J(1,4))*sin(x(1,1)), sin(J(1,4))*sin(J(1,1)),J(1,3)*cos(J(1,1));
     sin(J(1,1)), cos(J(1,4))*cos(J(1,1)), -sin(J(1,4))*cos(J(1,1)),J(1,3)*sin(J(1,1));
     0,sin(J(1,4)),cos(J(1,4)),J(1,2);
     0,0,0,1]; 

然后,

T2 = [cos(J(2,1)), -cos(J(2,4))*sin(x(2,1)), sin(J(2,4))*sin(J(2,1)),J(2,3)*cos(J(2,1));
     sin(J(2,1)), cos(J(2,4))*cos(J(2,1)), -sin(J(2,4))*cos(J(2,1)),J(2,3)*sin(J(2,1));
     0,sin(J(2,4)),cos(J(2,4)),J(2,2);
     0,0,0,1];

此过程将继续,直到创建了所有T矩阵。

到目前为止,这是我的代码:

function EE582_ASSIGNMENT2_Q4()

k = input('Enter the number of link and joint parameters, k: ');

x = zeros(k,4);


for n = 1:k
    disp(['Row: ', num2str(n)])
    fprintf('Arrange the Kinematic Parameters in the following syntax: [theta_%d,d_%d,l_%d,alpha_%d]\n',n,n,n,n);
    x(n,:) = input('Enter the Parameters: ');

    for v = 1:k 
        T{v} = [cos(x(v,1)), -cos(x(v,4))*sin(x(v,1)), sin(x(v,4))*sin(x(v,1)),x(v,3)*cos(x(v,1));
                sin(x(v,1)), cos(x(v,4))*cos(x(v,1)), -sin(x(v,4))*cos(x(v,1)),x(v,3)*sin(x(v,1));
                0,sin(x(v,4)),cos(x(v,4)),x(v,2);
                0,0,0,1]
    end

end

1 个答案:

答案 0 :(得分:0)

我可能会以不同的方式解决这个问题 - 为什么不将x作为函数输入参数?然后拨打T = {FUNCTION NAME}(x)

但是按照你的路线,这应该有效:

function [T] = EE582_ASSIGNMENT2_Q4()

k = input('Enter the number of link and joint parameters, k: ');

x = zeros(k,4);


for n = 1:k
  disp(['Row: ', num2str(n)])
  fprintf('Arrange the Kinematic Parameters in the following syntax: [theta_%d,d_%d,l_%d,alpha_% d]\n',n,n,n,n);
  x(n,:) = input('Enter the Parameters: ');
end
for v = 1:k 
  T{v} = [cos(x(v,1)), -cos(x(v,4))*sin(x(v,1)), sin(x(v,4))*sin(x(v,1)),x(v,3)*cos(x(v,1));
          sin(x(v,1)), cos(x(v,4))*cos(x(v,1)), -sin(x(v,4))*cos(x(v,1)),x(v,3)*sin(x(v,1));
          0,sin(x(v,4)),cos(x(v,4)),x(v,2);
          0,0,0,1];
end

你的问题在于循环变量。调用此函数所需的sytax是:T = EE582_ASSIGNMENT2_Q4,其中T可以是您想要的任何变量名称。