how to calculate a rotation matrix using a range of angles and store for each angle?

Discussion in 'MATLAB' started by Josh, Feb 28, 2011.

  1. Josh

    Josh Guest

    Hi,

    I am trying to calculate the below transformation matrix over 360 degrees, i.e for theta = 1:1:360. I then want each of these 360 matrices stored so that i can multiply each one with a similar set of matrices. However I can't seem to get around the matrix sizing and storing discrepancies. I have tried using 'cell' to create an array of 360 by 1 empty matrices and then writing a function to calculate the below matrix and trying to get each result into the array but i get the error message 'too many output arguments'. Does any one know how to fix this or just another way to tackle the problem? Any help would be really appreciated.


    T= [cos(theta) -sin(theta) 0 a;
    sin(theta)*cos(alpha) cos(theta)*cos(alpha) -sin(alpha) -sin(alpha)*d;
    sin(theta)*sin(alpha) cos(theta)*sin(alpha) cos(alpha) cos(alpha)*d;
    0 0 0 1 ];
     
    Josh, Feb 28, 2011
    #1
    1. Advertisements

  2. Josh

    ImageAnalyst Guest

     
    ImageAnalyst, Feb 28, 2011
    #2
    1. Advertisements

  3. Josh

    Josh Guest

    By the way in case it's not clear, the transformation matrix is a 4x4 matrix of which only some elements depend on theta
     
    Josh, Feb 28, 2011
    #3
  4. Josh

    Josh Guest

    No it doesn't you're right, all I want is the numeric values but I can't work out how to store them all as I can't use say

    for i=1:360
    M(i) = func(i)
    end

    as the function returns a matrix and also because i will have to convert i to radians,
    i,e i=0:0.1:3.14 and these are not all integers and that doesn't work??
     
    Josh, Feb 28, 2011
    #4
  5. Josh

    Josh Guest

    No it doesn't you're right, all I want is the numeric values but I can't work out how to store them all as I can't use say

    for i=1:360
    M(i) = func(i)
    end

    as the function returns a matrix and also because i will have to convert i to radians,
    i,e i=0:0.1:3.14 and these are not all integers and that doesn't work??
     
    Josh, Feb 28, 2011
    #5
  6. Josh

    Matt J Guest

    You could store as a 3D array:

    M=zeros(4,4,360); %pre-allocate

    for i=1:360
    M:),:,i) = func(i*pi/180)
    end

    Once you have it in this form, you can use MTIMESX to multiply something with each 4x4 matrix in the stack.

    http://www.mathworks.com/matlabcent...atrix-multiply-with-multi-dimensional-support

    Finally, an alternative to computing i*pi/180 is to define func() using trig functions COSD, SIND, etc... which accept angles in degrees.
     
    Matt J, Feb 28, 2011
    #6
  7. Josh

    Josh Guest

    Thanks for the cosd, sind info, didn't know these were available so that helps!

    Unfortunately using your 'for' loop with the 'M' zero array still gives the error of

    'Too many output arguments'

    due to my function 'linktrans'.

    linktrans simply calculates the transformation matrix(using alpha as the angle not theta) see below. Is this is because matlab is trying to multiply the 'i' 1x360 matrix with the 4x4 matrix?? Do you know how to solve this error?

    Many Thanks


    ....................................................
    function linktrans(alpha)

    theta=0;
    a=20;
    d=0;

    T= [cosd(theta) -sind(theta) 0 a;
    sind(theta)*cosd(alpha) cosd(theta)*cosd(alpha) -sind(alpha) -sind(alpha)*d;
    sind(theta)*sind(alpha) cosd(theta)*sind(alpha) cosd(alpha) cosd(alpha)*d;
    0 0 0 1 ];

    ............................................................
     
    Josh, Feb 28, 2011
    #7
  8. Josh

    Matt J Guest

    ===============

    You haven't told linktrans to output anything. Rewrite the first line as follows

    function T=linktrans(alpha)
     
    Matt J, Feb 28, 2011
    #8
  9. Josh

    Josh Guest

    Oh I see, thank you very much for that! I now have what I wanted
     
    Josh, Mar 1, 2011
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.