How can I multiply 2 matrices by a coefficient and then sum the results?

66 Views Asked by At

I would like to merge 2 rotation matrices from a gyroscope and an accelerometer, using a complementary filter :

orientationMatrix = 0.98 * gyroRotationMatrix + 0.02 * accRotationMatrix

The rotation matrices have the following format:

A1 A2 A3         B1 B2 B3
A4 A5 A6   AND   B4 B5 B6
A7 A8 A9         B7 B8 B9

Do you know how can I multiply a matrix by a coefficient (0.98 and 0.02) and then sum the results?

I tried :

A1 * 0.98 + B1 * 0.02         A2 * 0.98 + B2 * 0.02         A3 * 0.98 + B3 * 0.02
A4 * 0.98 + B4 * 0.02         A5 * 0.98 + B5 * 0.02         A6 * 0.98 + B6 * 0.02
A7 * 0.98 + B7 * 0.02         A8 * 0.98 + B8 * 0.02         A9 * 0.98 + B9 * 0.02

But it gave me weird results. What is my mistake?

2

There are 2 best solutions below

1
On BEST ANSWER
  1. Multiplying a matrix with a scalar is done by multiplying each entry of the matrix with the scalar.

  2. Then the two resulting matrices can be added up. This is done by adding up each entry of the matrices according to their entry coordinates.

Step 1:

$\begin{bmatrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{bmatrix} \times 0.98 = \begin{bmatrix}0.98 & 0 & 0\\0 & 0.98 & 0\\0 & 0 & 0.98\end{bmatrix} $

$\begin{bmatrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{bmatrix} \times 0.02 = \begin{bmatrix}0.02 & 0 & 0\\0 & 0.02 & 0\\0 & 0 & 0.02\end{bmatrix} $

Step 2:

$\begin{bmatrix}0.98+0.02 & 0+0 & 0+0\\0+0 & 0.98+0.02 & 0+0\\0+0 & 0+0 & 0.98+0.02\end{bmatrix}$

gives:

$\begin{bmatrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{bmatrix}$

edit: I used your initial example, but you can generalize this approach to matrices with different values as well.

1
On

You have correctly written down each of the columns of the resulting sum. Your final matrix will be $$\begin{bmatrix} 1&0&0\\0&1&0\\0&0&1\end{bmatrix}$$

An easier way to do the addition is to take advantage of the fact that both of the summand matrices are the same. Hence, we have $$=.98\begin{bmatrix} 1&0&0\\0&1&0\\0&0&1\end{bmatrix}+.02\begin{bmatrix} 1&0&0\\0&1&0\\0&0&1\end{bmatrix}$$ $$=(.98+.02)\begin{bmatrix} 1&0&0\\0&1&0\\0&0&1\end{bmatrix}$$ $$=\begin{bmatrix} 1&0&0\\0&1&0\\0&0&1\end{bmatrix}$$