# quaternion conversion

## q2str, q2vec, q2tr, tr2q

Conversion between quaternion and other types of data.

### Syntax

• qstr = q2str(q)
• v = q2vec(q)
• [T,R] = q2tr(q)
• q = tr2q(R)
• q = tr2q(T)

### Input Arguments

• q — a quaternion s <v1,v2,v3>
• R — 3 x 3 rotation matrix
• T — 4×4 homogeneous transformation matrix

### Output Arguments

• qstr — a string that represents quaternion value s <v1,v2,v3>
• v — a vector representation [s v1 v2 v3] of quaternion s <v1,v2,v3>
• R — 3 x 3 rotation matrix
• T — 4×4 homogeneous transformation matrix
• q — a quaternion s <v1,v2,v3>

### Description

A quaternion is a compact method of representing a 3D rotation that has computational advantages including speed and numerical robustness. A quaternion has 2 parts, a scalar s, and a vector v and is typically written: q = s <vx, vy, vz> This group of functions convert between quaternion and other types of data: string, vector, rotation/homogeneous transform matrix.

• qstr = q2str(q ) converts a quaternion s <v1, v2, v3> to a string
• v = q2vec(q) converts a quaternion s <v1, v2, v3> to a vector v = [s v1 v2 v3].
• [T,R] = q2tr(q) returns homogenous transform and rotation matrices corresponding to the unit quaternion q.
• q = tr2q(R), q = tr2q(T) returns a unit quaternion corresponding to a rotation matrix and homogeneous transform matrix, respectively.

### Examples

```-->q = quaternion([1 -2 0 4]); -->q2str(q) ans = 1.000000, < -2.000000, 0.000000, 4.000000> -->q2vec(q) ans = 1. - 2. 0. 4. -->T = q2tr(qunit(q)) T = - 0.5238095 - 0.3809524 - 0.7619048 0. 0.3809524 - 0.9047619 0.1904762 0. - 0.7619048 - 0.1904762 0.6190476 0. 0. 0. 0. 1. -->tr2q(T) ans = s: 0.2182179 v: [-0.4364358,0,0.8728716]```