1
votes

I'm trying to work with vector data to calculate the angle between two vectors. I'm using atan2 which operates in the x-plane but I want to remove points from the y-plane.

How do I rotate my data so that my y-plane is my x-plane, do my analysis, and then rotate it back? I have the calculations already for my analysis, just don't know how to rotate the vector data while retaining each position.

3
An example would really clarify the problem here - vectors in a mathematical sense are different from vectors in a matlab sense. How are you representing your data? - xenoclast
sure. my vectors are actually text documents with three columns: XYZ positions. So I want to rotate the entire model 90 degrees and ensure that all data dimensionality is retained. I'm assuming that atan2 is operating about the x-axis since after running and filtering out data it removed x features not y features - user3433572
let me know if I need any more clarification!! I'd be happy to add more detail if I left anything out. - user3433572
Rotate 90 degrees with respect to what axis and in what direction? Clockwise or counter-clockwise? If your points are represented in 3D space, a simple application of the rotation matrix will suffice. - rayryeng
rotate points 90 degrees clockwise about the x-axis. i believe that would make my x points in the y- direction - user3433572

3 Answers

6
votes

Assuming that your data points are in a N x 3 matrix where N is the total number of points that you have, simply apply a rotation matrix to each point.

You can rotate a point by performing a very simple matrix multiplication. Given a point as a 3 element column vector X, the output point X' is simply:

X' = R*X

R is a rotation matrix. There are three rotation matrices depending on which axis you want to rotate with respect with. In your case, you want to rotate 90 degrees clockwise about the x-axis. The general forms for rotating about each axis in a counter-clockwise direction are given here:

Source: Wikipedia

In your case, you want the first matrix. Because you want to rotate 90 degrees clockwise, you would specify the angle to be -90 degrees. As such, construct your rotation matrix for the x-axis as so:

R = [1 0 0; 0 cosd(-90) -sind(-90); 0 sind(-90) cosd(-90)];

Also, cos(90) = 0 and sin(-90) = -1 assuming degrees, so this really simplifies to:

R =

     1     0     0
     0     0     1
     0    -1     0

To undo the rotation you made, simply apply a 90 degree counter-clockwise rotation, and so substitute 90 degrees into the above expression. Doing this gives us:

R =

     1     0     0
     0     0    -1
     0     1     0

However, your points are in a N x 3 matrix. We want to represent each column as a point, and so what you need to do is transpose your matrix, then do the multiplication, and then transpose back (if desired). Assuming your points are stored in X, just do this:

R = [1 0 0; 0 cosd(-90) -sind(-90); 0 sind(-90) cosd(-90)];
Xrotate = (R*X.').';

Xrotate will contain a N x 3 matrix that contains your rotated points. Each column will be a rotated point given the source point in the original X matrix. It should be noted that each row of X gets rotated and placed into a column in Xrotate and we'll need to transpose this result to get it back into the shape of X. Do your processing, and when you're ready you can rotate your points back. Assuming that Xrotate is N x 3, simply do this:

R2 = [1 0 0; 0 cosd(90); -sind(90); 0 sind(90); cosd(90)];
Xout = (R2*Xrotate.').';

Xout contains the rotation and is re-transposed back so that each row is a point and each column is a dimension.

-1
votes

Anyway, the best way to compute the angle between 2 vectors is using the dot product: u · v = norm(u,2) * norm(v,2) * cos(angle)

You can compute it really fast this way u·v are 2 sums and 3 multiplications, and the norm function has 3 multiplications, 2 sums and a square root, and 2 divisions previous to acos(). Count them as 20 operations to get the angle, much better than multiplying matrices to vectors, changing the coordinates system, aplying the operations you were trying...

-1
votes

Quoting from the manual:

Syntax

rotate(h,direction,alpha)
rotate(...,origin)

Description

The rotate function rotates a graphics object in three-dimensional space.

rotate(h,direction,alpha) rotates the graphics object h by alpha degrees. direction is a two- or three-element vector that describes the axis of rotation in conjunction with the origin of the axis of rotation. The default origin of the axis of rotation is the center of the plot box. This point is not necessarily the origin of the axes.

Positive alpha is defined as the righthand-rule angle about the direction vector as it extends from the origin of rotation.

If h is an array of handles, all objects must be children of the same axes.

rotate(...,origin) specifies the origin of the axis of rotation as a three-element vector [x0,y0,z0].