English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Matrices

Le langage R offre un type de matrice pour la recherche en algèbre linéaire, cette structure de données est très similaire aux tableaux bidimensionnels d'autres langages, mais R offre un soutien de calcul matriciel au niveau du langage.

Les éléments de la matrice peuvent être des nombres, des symboles ou des expressions mathématiques.

Une matrice MxN est composée de M lignes (row) et N colonnes (column)éléments alignés en rangée.

Voici un tableau rectangulaire composé de 6 éléments numériques 2 row 3 Matrice de colonnes :

Les matrices en langage R peuvent être créées à l'aide de la fonction matrix(), la syntaxe est la suivante :

matrix(data = NA, nrow = 1,ncol = 1,byrow = FALSE, dimnames = NULL

Description des paramètres :

  • data Vector, données de la matrice

  • nrow Nombre de lignes

  • ncol Nombre de colonnes

  • byrow Valeur logique, FALSE pour aligner par colonnes, TRUE pour aligner par lignes

  • dimname Définir les noms des lignes et des colonnes

Créer une matrice numérique :

# byrow est TRUE, les éléments sont alignés par lignes
M <- matrix(c(3:14), nrow = 4,byrow = TRUE
print(M)
# Ebyrow est FALSE, les éléments sont alignés par colonnes
N <- matrix(c(3:14), nrow = 4,byrow = FALSE
print(N)
# Define the names of rows and columns
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")
P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(P)

The output result of executing the above code is:

[[1] [,2] [,3]
[1,]    3    4    5
[2,]    6    7    8
[3,]    9   10   11
[4,]   12   13   14
     [[1] [,2] [,3]
[1,]    3    7   11
[2,]    4    8   12
[3,]    5    9   13
[4,]    6   10   14
     col1 col2 col3
row1    3    4    5
row2    6    7    8
row3    9   10   11
row4   12   13   14

Matrice transposée

La fonction t() du langage R permet de permuter les lignes et les colonnes d'une matrice.

Par exemple, un matrice de m lignes et n colonnes peut être convertie en une matrice de n lignes et m colonnes en utilisant la fonction t().

# Créer un 2 row 3 column matrix
M = matrix(c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE
print(M)
     [[1] [,2] [,3]
[1,]    2    6    5
[2,]    1   10    4
# Convertir en 3 row 2 column matrix
print(t(M))

The output result of executing the above code is:

     [[1] [,2] [,3]
[1,]    2    6    5
[2,]    1   10    4
[1-----Conversion-----"
     [[1] [,2]
[1,]    2    1
[2,]    6   10
[3,]    5    4

Access Matrix Elements

If you want to get the matrix element, you can use the column index and row index of the element, similar to the coordinate form.

# Define the names of rows and columns
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")
# Create matrix
P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(P)
# Get the element at the first row and third column
print(P[1,3))
# Get the element at the fourth row and second column
print(P[4,2))
# Get the second row
print(P[2,])
# Get the third column
print(P[,3))

The output result of executing the above code is:

col1 col2 col3
row1    3    4    5
row2    6    7    8
row3    9   10   11
row4   12   13   14
[1] 5
[1] 13
col1 col2 col3 
    6    7    8 
row1 row2 row3 row4 
    5    8   11   14

Matrix Calculation

Matrices of the same size (rows and columns) can be added or subtracted, which is to perform addition or subtraction on each element at each position. The matrix multiplication is more complex. Two matrices can be multiplied when and only when the number of columns of the first matrix is equal to the number of rows of the second matrix.

Matrix addition and subtraction

# Create 2 row 3 column matrix
matrix1 <- matrix(c(7, 9, -1, 4, 2, 3), nrow = 2)
print(matrix1)
matrix2 <- matrix(c(6, 1, 0, 9, 3, 2), nrow = 2)
print(matrix2)
# Two matrices addition
result <- matrix1 + matrix2
cat("Addition result:","\n")
print(result)
# Two matrices subtraction
result <- matrix1 - matrix2
cat("Subtraction result:","\n")
print(result)

The output result of executing the above code is:

[[1] [,2] [,3]
[1,]    7   -1    2
[2,]    9    4    3
     [[1] [,2] [,3]
[1,]    6    0    3
[2,]    1    9    2
Addition result: 
     [[1] [,2] [,3]
[1,]   13   -1    5
[2,]   10   13    5
Subtraction result: 
     [[1] [,2] [,3]
[1,]    1   -1   -1
[2,]    8   -5    1

Matrix multiplication and division

# Create 2 row 3 column matrix
matrix1 <- matrix(c(7, 9, -1, 4, 2, 3), nrow = 2)
print(matrix1)
matrix2 <- matrix(c(6, 1, 0, 9, 3, 2), nrow = 2)
print(matrix2)
# Two matrices multiplication
result <- matrix1 * matrix2
cat("Multiplication result:","\n")
print(result)
# Two matrices division
result <- matrix1 / matrix2
cat("Division result:","\n")
print(result)

The output result of executing the above code is:

[[1] [,2] [,3]
[1,]    7   -1    2
[2,]    9    4    3
     [[1] [,2] [,3]
[1,]    6    0    3
[2,]    1    9    2
Multiplication result: 
     [[1] [,2] [,3]
[1,]   42    0    6
[2,]    9   36    6
Division result: 
         [[1] [,2] [,3]
[1,] 1.166667      -Inf 0.6666667
[2,] 9.000000 0.4444444 1.5000000