1
$\begingroup$

I am asked to generate a random matrix that satisfy the requirements for rotation matrices. Since I don't really know how to do that, I thought of creating a random quaternion(hopefully, there is no error there. If there is, please let me know). I start by randomizing an angle between 0 and 2$\pi$ and a vector $t$ (axis) . then I get the unit vector of t. Now I want to convert the quaternion to a rotation matrix. So is there a sample of code I could use to do so? Anyhow, Thanks!

    rg=2*pi*[0,1];     
    theta=diff(rg)*rand(1,1)+rg(1);

    t=rand(3,1)*2-1;

    q = [cos(theta/2),t(1)* sin(theta/2),t(2)* sin(theta/2),t(3)* sin(theta/2)];
    R = zeros(3);
$\endgroup$
  • $\begingroup$ A 3D rotation matrix, right? $\endgroup$ – Parcly Taxel May 13 '17 at 15:43
  • $\begingroup$ @ParclyTaxel Yes $\endgroup$ – Abu Bakr May 13 '17 at 16:43
1
$\begingroup$

Here's one way:

Choose $X,Y,Z$ independently with a standard normal distribution (i.e. $\dfrac 1 {\sqrt{2\pi}} e^{-x^2/2} \, dx$).

Then replace $(X_1,Y_1,Z_1)$ with $\dfrac{(X_1,Y_1,Z_1)}{\sqrt{X_1^2+Y_1^2 + Z_1^2}}.$

Then choose $(X_2,Y_2,Z_2)$ independently from the standard normal distribution. First replace $(X_2,Y_2,Z_2)$ with $(X_2,Y_2,Z_2)$ minus the orthogonal projection of $(X_2,Y_2,Z_2)$ onto $(X_1,Y_1,Z_1),$ and finally replace this new $(X_2,Y_2,Z_2)$ with $\dfrac{(X_2,Y_2,Z_2)}{\sqrt{X_2^2+Y_2^2+Z_2^2}}.$

Then choose $(X_3,Y_3,Z_3)$ independently from the standard normal distribution. First replace $(X_3,Y_3,Z_3)$ with $(X_3,Y_3,Z_3)$ minus the sum of the orthogonal projections of $(X_3,Y_3,Z_3)$ onto $(X_1,Y_1,Z_1)$ and $(X_2,Y_2,Z_2),$ and finally replace this new $(X_3,Y_3,Z_3)$ with $\dfrac{(X_3,Y_3,Z_3)}{\sqrt{X_3^2+Y_3^2+Z_3^2}}.$

Now you have a randomly chosen orthonormal basis of $\mathbb R^3$. Let its members be the three columns of your matrix. The determinant of this matrix will be either $1$ or $-1$. If it is $-1$ then interchange two of its columns. The result will be a randomly chosen rotation matrix.

$\endgroup$
  • $\begingroup$ Thanks a lot!! this really helped and the code now works! $\endgroup$ – Abu Bakr May 16 '17 at 11:43
  • $\begingroup$ @AbuBakr : I'm glad it helps. $\endgroup$ – Michael Hardy May 16 '17 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.