Rotate matrix +/- radian / degree - julia

Here is a matrix:
A = reshape(collect(1:81), (9, 9))
How may I rotate this matrix in +/- radians/degrees?
Lets say I wish to do a 45 degree rotation
I guess imagine all the 1's in the middle lining up at a 45 degree angle...
Any packages to recommend?

Related

How to combine two 3x3 rotation matrices to align accelerometer axes with another coordinate system

I am trying to align the X, Y, Z axes from an accelerometer to the X, Y, Z axes of a vehicle.
My approach consists of two steps:
1 step: Find a rotation matrix Rz to align the Z axes of both coordinate systems. I know the acceleration vector of the vehicle is A_veh=(0,0,1000) (values in mg) when is stopped on a level surface, so I measure the acceleration reported by the accelerometer at that very moment and I get my vector A_acc=(6,64,1016) (values in mg), so I proceed to find a rotation matrix to align A_acc with the Z-axis of the vehicle (A_veh). I form the ration matrix by using the implementation of the Rodriguez formula listed here and we find out:
`Rz[][] = 0.999983 -0.000185 -0.005894
-0.000185 0.998022 -0.062866
0.005894 0.062866 0.998005`
To verify the rotation matrix I multiply the A_acc vector by Rz and get an aligned vector A_z_aligned=(0.000000,0.000000,1018) (in mg) which is the expected since all the acceleration is on the Z-axis of the vehicle.
Image showing debug info about the process of getting Rz
2 step: Find a rotation matrix Rxy to align the XY axes of both coordinate systems. Measuring some acceleration of the vehicle with an external sensor, I get the following acceleration vector for the vehicle A_veh=(-323,0,1009) and from the accelerometer, I get A_acc=(-11,-322,1009) (in mg), so repeating the procedure from step 1 I get the rotation matrix Rxy:
`Rxy[][] = 0.954307 0.135202 -0.266494
-0.050168 0.951624 0.303143
0.294151 -0.275922 0.914924`
And to verify this rotation matrix I multiply the A_acc vector by Rxy and get an aligned vector A_xy_aligned=(-322,0,1008) which is the expected.
Image showing debug info about the process of getting Rxy
Question: How can I "combine" the two rotation matrices in order to have one rotation matrix that allows me to align any raw acceleration vector read from the accelerometer to the vehicle axes?
I have tried multiplying the raw acceleration vectors by Rz and then by Rxy and it seems it does not work.
I also tried multiplying both matrices Rz*Rxy=R and use R to align the raw vectors but it does not work either.
I need some help with this topic.
Thanks,

Direction of resultant vector

in this question- a motorboat is racing towards north at 25 km/hr and the water current in that region is 10 km/hr in a direction 60 degrees east of south. find the resultant velocity and direction of resultant velocity of the boat.
For getting the velocity i just used the cosin formula and got 22 km/hr approx but i cant seem to understand how to calculate the direction when the angle between the vectors is 120. it is easy when the angle is less that or equal to 90 when we just find tan theta where theta is the angle between the vectors. but how do we go about doing this?
The velocity is as actually a vector with a direction. If you sketch this you will have a vector addition (build a parallelogram) of The boats speed vector and the water flows speed vector. The directions of the vectors you can extract from the given direction. For example take north as (1,0) direction and draw an arrow in the correct length (speed).
v_boat = 25 * (1,0)
v_flow = 10 * ...
v_sum = v_boat + v_flow
v_sum_magnitude = magnitude of v_sum
For the background:
Velocity is working with the principle of superposition.

Rotate normal vector onto axis plane in 4D

The problem I need to solve is to rotate a 4-simplex given in 4D on the hyperplane with normal vector (1, 1, 1, 1) so that I can draw it in 3D. For instance I need to know the rotation for the regular one having vertices e_i (that is the coordinate vectors), and all its sub-simplices after division.
In order to understand the problem, let's go one dimension back. If you have a 3-simplex in 3D on the hyperplane with normal vector (1, 1, 1) like here (http://upload.wikimedia.org/wikipedia/commons/thumb/3/38/2D-simplex.svg/150px-2D-simplex.svg.png), one can follow the idea of Nosredna to the question
Rotate normal vector onto axis plane
It works fine in 3D, but in 4D there is no cross products, so I cannot extend this answer to my question. On the other hand using rotation matrices I managed to rotate the simplex around the x axes by -45 degree, then rotating around the y axes by around 35 degree (atan(sqrt(2)/2) using the coordinate rotation matrices (http:// upload.wikimedia.org/math/2/8/5/2851c9dc2031127e6dacfb84b96446d8.png).
I also tried to calculate a rotation matrix from axes rotations like in http://ken-soft.com/2009/01/08/graph4d-rotation4d-project-to-2d/ but I could not find out what should be the angles to use. So I used R=rotXU*rotYU*rotZU with the angles pi/4, -atan(sqrt(2)/2, and -pi/6, which looked good, but somehow the result wasn't ok.
Sorry, I could not put the images directly as I'm a newbie...
Thank you for any answer!
There are no rotation axes in 4D, for the same reason there is no cross product: the group of 4D rotations is 6-dimentional, and the space you are rotating is 4-dimentional. Imagine, for example, a simultaneous rotation in XY plane and ZT plane: it has no non-zero stationary vectors, and therefore no axes.
The most appropriate thing to do is to work with the usual transormation matrix, which is applicable in any dimention N:
[ a11 ... a1N d1 ]
...
[ aN1 ... aNN dN ]
[ 0 ... 0 1 ]
Here d1 ... dN represent translations, and the NxN submatrix aIJ represent rotations, dilations, projections, and mirroring. To limit to rotations only make this matrix orthogonal: its product with its own transpose should be the unit matrix. This is a common practice for N=2 and N=3, you just do the same for N=4.
To find the appropriate rotation matrix in your case write down add the requirement that the entire 4th row of an orthogonal 4x4 matrix is zero, and that would give you a bunch of solutions, each being an acceptable answer to your question.

How to transform a co-ordinate value in 45 deg-135 deg co-ordinate system to earth co-ordinate system?

I get a series of square binary images as in the picture below,
I want to find the red point, which is the point of intersection of four blocks (2 black and 2 white). For doing so, I use to get the sum of all pixel values along the diagonal directions of the square image, which is 45 deg and 135 deg respectively. The intersection of maximum pixel sum 45 deg line and minimum pixel sum 135 deg line is where my red point is.
Now that I get the co-ordinate of the red point in 45 deg-135 deg co-ordinate system, how to I transform them to earth co-ordinates?
In other words, say I have a point in 45deg-135deg co-ordinate system; How do I find the corresponding co-ordinate values in x-y co-ordinate system? What is the transformation matrix?
some more information that might help:
1) if the image is a 60x60 image, I get 120 values in 45deg-135deg system, since i scan each row followed by column to add the pixels.
I don't know much about matlab, but in general all you need to do is rotate your grid by 45 degrees.
Here's a helpful link; shows you the rotation matrix you need
wikipedia rotation matrix article
The new coordinates for a point after 2D rotation look like this:
x' = x \cos \theta - y \sin \theta.
y' = x \sin \theta + y \cos \theta.
replace theta with 45 (or maybe -45) and you should be all set.
If your red dot starts out at (x,y), then after the -45 degree rotation it will have the new coordinates (x',y'), which are defined as follows:
x' = x cos(-45) - y sin (-45)
y' = x sin (-45) + y cos (-45)
Sorry when I misunderstood your question but why do you rotate the image? The x-value of your red point is just the point where the derivative in x-direction has the maximum absolute value. And for the y-direction it is the same with the derivative in y-direction.
Assume you have the following image
If you take the first row of the image it has at the beginning all 1 and the for most of the width zeroes. The plot of the first column looks like this.
Now you convolve this line with the kernel {-1,1} which is only one nested loop over your line and you get
Going now through this result and extracting the position of the point with the highest value gets you 72. Therefore the x-position of the red point is 73 (since the kernel of the convolution finds the derivative one point too soon).
Therefore, if data is the image matrix of the above binary image then extracting your red point position is near to one line in Mathematica
Last[Transpose[Position[ListConvolve[{-1, 1}, #] & /#
{data[[1]],Transpose[data][[1]]}, 1 | -1]]] + 1
Here you get {73, 86} which is the correct position if y=0 is the top row. This method should be implemented in a few minutes in any language.
Remarks:
The approximated derivative which is the result of the convolution can either be negative or positive. This depends whether it is a change from 0 to 1 or vice versa. If you want to search for the highest value, you have to take the absolute value of the convolution result.
Remember that the first row in the image matrix is not always in top position of the displayed image. This depends on the software you are using. If you get wrong y values be aware of that.

How do I break a moving objects speed up into its X and Y velocity given a fixed speed/angle?

Math escapes me today.
How do I find the X speed and the Y speed of an object if it is going at a defined speed (say, 5 pixels/second) at a 45 degree angle?
So always 5 pixels/sec and always 45 degrees?
The general case is
velx=cos(a)*vel;
vely=sin(a)*vel;
a is angle, usually in radians, so convert from degrees, and the signs (positive/negative) will depend on your coordinate system.
Crazy fact from the 1980s: In the old days, we used lookup tables for sin and cos!
Edited: Made my axes more conventional thanks to comment below. x is positive to your right. y is positive up. 45 degrees is to the northeast. If you have something else, let me know.
It will be
Vx=VCos#
Vy=Vsin#
So in your case it will be Vx=5*cos45 and Vy=5*sin45
At 45 angle value of Cos & Sin is same i.e 1/root 2.
Note: If you are doing any math stuff in programming then have a look at Vecmath lib.
At a 45 degree angle, an object is going sqrt(2)/2 of the speed along each axis. Generally, you can do it with sin and cosine, but for specific angles like this you can do it just by knowing pythagorean triangles.
In a right triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides. You know the hypotenuse is V. You also know that the other two sides equal each other. That means that V^2 = Vx^2 * 2. This means that Vx = sqrt(V^2/2), which equals V * sqrt(1/2).

Resources