OpƩrations matricielles dans Octave
Dans cette leçon, je vais expliquer comment effectuer les principales opérations matricielles dans Octave avec quelques exemples pratiques.
Avant de commencer, créez deux tableaux de matrices.
Écrivez une matrice M1 avec deux rangées et deux colonnes. C'est une matrice carrée.
>> M1=[1 4;2 3]
M 1 =
1 4
2 3
Maintenant, écrivez une autre matrice carrée M2 avec deux rangées et deux colonnes.
>> M2=[3 1;7 5]
M2 =
3 1
7 5
Voici quelques opérations du calcul matriciel
- Addition de matrices
- Soustraction de matrices
- Multiplication de matrices
- Multiplication élément par élément des matrices
- Multiplication matricielle par un scalaire
- Division de matrices
- Division élément par élément des matrices
- Division matricielle par un scalaire
- Exponentiation élément par élément de la matrice
- Déterminant de la matrice
- Rang
- Matrice transposée
- Matrice inverse
Addition de matrices
Pour additionner deux matrices, tapez M1 + M2
>> M1+M2
ans =
4 5
9 8
$$ M1 + M2 = \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} + \begin{pmatrix} 3 & 1 \\ 7 & 5 \end{pmatrix} = \begin{pmatrix} 1+3 & 4+1 \\ 2+7 & 3+5 \end{pmatrix} = \begin{pmatrix} 4 & 5 \\ 9 & 8 \end{pmatrix} $$
Soustraction de matrices
Pour calculer la différence entre deux matrices, tapez M1-M2
>> M1-M2
ans =
-2 3
-5 -2
$$ M1 - M2 = \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} - \begin{pmatrix} 3 & 1 \\ 7 & 5 \end{pmatrix} = \begin{pmatrix} 1-3 & 4-1 \\ 2-7 & 3-5 \end{pmatrix} = \begin{pmatrix} -2 & 3 \\ -5 & -2 \end{pmatrix} $$
Multiplication de matrices
Pour calculer le produit de deux matrices, tapez M1 * M2
>> M1*M2
ans =
31 21
27 17
$$ M1 \cdot M2 = \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} \cdot \begin{pmatrix} 3 & 1 \\ 7 & 5 \end{pmatrix} = \begin{pmatrix} 1 \cdot 3 + 4 \cdot 7 & 1 \cdot 1 + 4 \cdot 5 \\ 2 \cdot 3 + 3 \cdot 7 & 2 \cdot 1 + 3 \cdot 5 \end{pmatrix} = \begin{pmatrix} 31 & 21 \\ 27 & 17 \end{pmatrix} $$
Rappelez-vous que vous ne pouvez calculer le produit de deux matrices que si le nombre de colonnes de la première matrice est égal au nombre de rangées de la seconde matrice.
Multiplication élément par élément des matrices
C'est un autre type de multiplication matricielle. Cette opération calcule le produit des éléments des tableaux de matrices qui occupent la même position.
Pour calculer ce type de multiplication, vous devez utiliser le symbole .*
>> M1 .* M2
ans =
3 4
14 15
Dans la multiplication élément par élément, les deux matrices doivent avoir le même nombre de rangées et de colonnes.
$$ M1 \ .* \ M2 = \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} \ .* \ \begin{pmatrix} 3 & 1 \\ 7 & 5 \end{pmatrix} = \begin{pmatrix} 1 \cdot 3 & 4 \cdot 1 \\ 2 \cdot 7 & 3 \cdot 5 \end{pmatrix} = \begin{pmatrix} 3 & 4 \\ 14 & 15 \end{pmatrix} $$
Multiplication matricielle par un scalaire
Pour multiplier une matrice par un nombre scalaire, par exemple k = 2, tapez 2 * M1
>> 2*M1
ans =
2 8
4 6
Tous les éléments de la matrice sont multipliés par le nombre scalaire.
$$ 2 \cdot M1 = 2 \cdot \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} = \begin{pmatrix} 2 \cdot 1 & 2 \cdot 4 \\2 \cdot 2 & 2 \cdot 3 \end{pmatrix} = \begin{pmatrix} 2 & 8 \\4 & 6 \end{pmatrix} $$
Division de matrices
En algèbre linéaire, la division entre deux matrices est calculée en multipliant la première matrice par l'inverse de la seconde M1·M2-1.
Pour calculer cette opération dans Octave, tapez M1*inv(M2)
>> M1*inv(M2)
ans =
-2.87500 1.37500
-1.37500 0.87500
Alternativement, vous pouvez aussi taper M1/M2
>> M1/M2
ans =
-2.87500 1.37500
-1.37500 0.87500
Le résultat est le même.
Division élément par élément des matrices
C'est un autre type de division matricielle. Cette opération calcule le quotient entre les éléments des tableaux qui se trouvent à la même position.
Pour faire ce type de division, vous devez utiliser le symbole ./
>> M1 ./ M2
ans =
0.33333 4.0000
0.28571 0.6000
Dans la division élément par élément, les deux matrices doivent avoir le même nombre de rangées et de colonnes.
$$ M1 \ ./ \ M2 = \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} \ ./ \ \begin{pmatrix} 3 & 1 \\ 7 & 5 \end{pmatrix} = \begin{pmatrix} \frac{1}{3} & \frac{4}{1} \\ \frac{2}{7} & \frac{3}{5} \end{pmatrix} = \begin{pmatrix} 0.33333 & 0.28571 \\ 4 & 0.6 \end{pmatrix} $$
Division matricielle par un scalaire
Si vous voulez diviser une matrice par un nombre scalaire, par exemple k = 2, tapez M1 / 2
>> M1/2
ans =
0.50000 2.00000
1.00000 1.50000
Les éléments de la matrice sont divisés par le nombre scalaire.
$$ \frac{M1}{2} = \frac{1}{2} \cdot \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} = \begin{pmatrix} \frac{1}{2} \cdot 1 & \frac{1}{2} \cdot 4 \\ \frac{1}{2} \cdot 2 & \frac{1}{2} \cdot 3 \end{pmatrix} = \begin{pmatrix} 0.5 & 2 \\ 1 & 1.5 \end{pmatrix} $$
Exponentiation élément par élément de la matrice
Cette opération calcule l'exponentiation de tous les éléments de la matrice par le même exposant.
Pour effectuer ce type d'opération, vous devez utiliser le symbole .^
>> M1.^2
ans =
1 16
4 9
$$ M1 \ \text{.^} \ 2 = \begin{pmatrix} 1^2 & 4^2 \\ 2^2 & 3^2 \end{pmatrix} = \begin{pmatrix} 1 & 16 \\ 4 & 9 \end{pmatrix} $$
Déterminant de la matrice
Pour calculer le déterminant d'une matrice carrée, utilisez la fonction det()
>> det(M1)
ans = -5
$$ \text{det} (M1) = det \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} = 1 \cdot 3 - 4 \cdot 2 = -5 $$
Rang
Pour calculer le rang d'une matrice carrée, utilisez la fonction rank()
>> rank(M1)
ans = 2
Matrice transposée
Pour transposer une matrice, utilisez la fonction transpose()
>> transpose(M1)
ans =
1 2
4 3
Alternativement, vous pouvez transposer la matrice en ajoutant une apostrophe après le nom de la matrice
>> M1'
ans =
1 2
4 3
Dans la transposition, les rangées de la matrice deviennent des colonnes. $$ \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix}^T = \begin{pmatrix} 1 & 2 \\ 4 & 3 \end{pmatrix} $$
Matrice inverse
Pour calculer la matrice inverse, utilisez la fonction inv()
>> inv(M1)
ans =
-0.60000 0.80000
0.40000 -0.20000
La matrice inverse de M1 est une matrice qui, multipliée par M1, donne une matrice identité, c'est-à-dire une matrice avec des éléments égaux à 1 sur la diagonale principale et tous les autres éléments sont nuls. $$ M1 \cdot \text{inv} (M1) = \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} \cdot \begin{pmatrix} -0.6 & 0.8 \\ 0.4 & -0.2 \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} $$