# matlab - Create series of matrices from data contained within one matrix

itPublisher 分享于

I am trying to create a series of matrices from the data contained within a pre-defined matrix.

I have written some code which asks the user to specify the number of rows of a 4 column matrix.

Then the user is to input 4 elements for every row that they have specified which will thereby create a k x 4 matrix.

I.e if the user selects 3 rows then they will enter x1,x2,x3,x4 for row 1 and then y1,y2,y3,y3 for row 2 and then z1,z2,z3,z4 for row three, hence the matrix created becomes: `J = [x1,x2,x3,x4;y1,y2,y3,y3;z1,z2,z3,z4]`.

My problem is, is that I need to get the data out of matrix J to create a series of new matrices. The number of new matrices is dependent on the number of rows that matrix J consists of, i.e. if matrix J has 5 rows then I need to create 5 new matrices from the data contained within matrix J, e.g T1, T2, T3, T4, T5.

The newly created matrices (T) require elements from the J matrix like this:

``````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];
``````

And then,

``````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];
``````

This procedure continues until all T matrices have been created.

This is my code so far:

``````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
``````
matlab matrix extraction
edited Nov 29 '14 at 19:15 Cape Code 3,308 3 15 36 asked Nov 29 '14 at 18:55 Harpreet Lota 6 1      What's the problem with your code? What errors do you get? –  Trojanian Nov 30 '14 at 17:25      Well when i run the code, i obtain the following output on the command window: `>> EE582_ASSIGNMENT2_Q4 Enter the number of link and joint parameters, k: 1 Row: 1 Arrange the Kinematic Parameters in the following syntax: [theta_1,d_1,l_1,alpha_1] Enter the Parameters: [45,60,0,pi/2] T = [4x4 double] >>` therefore it would seem that a 4x4 "T" matrix is created but I do not know what is in it. however, I have adapted my code which seems to work by using the EVAL function but there does not seem to be a straightforward method of using the "T" matrices later for different operations –  Harpreet Lota Dec 1 '14 at 17:35      The modified code that I mentioned at the end of my previous comment is here: `function test_q() k = input('Enter the number of link and joint parameters, k: '); x = zeros(k,4); T = zeros(); 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 n = 1:k eval(sprintf('T%d = [cos(x(n,1)*(pi/180)), .....', n)); end end ` –  Harpreet Lota Dec 1 '14 at 18:06      Did you check the answer? –  Trojanian Dec 4 '14 at 23:14

|

I would probably go about this quite differently - why not have your `x` as a function input parameter? Then call `T = {FUNCTION NAME}(x)`?

But following your route, this should work:

``````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
``````

Your problems were with the looping variables. The sytax needed to call this function is: `T = EE582_ASSIGNMENT2_Q4`, where `T` can be whatever variable name you want.

answered Dec 1 '14 at 20:33 Trojanian 331 3 16

|

×
• 登录
• 注册

×