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

Matrizes em R

R language provides matrix types for the study of linear algebra, this data structure is very similar to two-dimensional arrays in other languages, but R provides language-level matrix operation support.

The elements in the matrix can be numbers, symbols, or mathematical expressions.

A  M  x N matrix is a matrix composed of M (row) rows and N (column)elements arranged in a rectangular array.

The following is a rectangular array composed of 6 a number of elements构成的 2 Linhas 3 Column matrix:

R language matrices can be created using the  matrix() function, with the syntax format as follows:

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

Parameter description:

  • data Vector, the data of the matrix

  • nrow Number of rows

  • ncol Number of columns

  • byrow Logical value, FALSE is arranged by column, TRUE is arranged by row

  • dimname Set the names of rows and columns

Create a numeric matrix:

# byrow is TRUE, elements are arranged by row
M <- matrix(c(3:14, 4, byrow = TRUE
print(M)
# Ebyrow is FALSE, elements are arranged by column
N <- matrix(c(3:14, 4, byrow = FALSE
print(N)
# Definir os nomes das linhas e colunas
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")
P <- matrix(c(3:14, 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(P)

O resultado da execução do código acima é:

[[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

Transpose matrix

The R language matrix provides the t() function to achieve the interchange of rows and columns of a matrix.

For example, if you have a matrix with m rows and n columns, you can use the t() function to convert it to a matrix with n rows and m columns.

# Create a 2 Linhas 3 Matriz de Colunas
M = matrix( c(2, 0,6, 0,5, 0,1, 0,10, 0,4, 2,ncol = 3,byrow = TRUE
print(M)
     [[1] [,2] [,3]
[1,]    2    6    5
[2,]    1   10    4
# Converter 3 Linhas 2 Matriz de Colunas
print(t(M))

O resultado da execução do código acima é:

     [[1] [,2] [,3]
[1,]    2    6    5
[2,]    1   10    4
[1】 "-----Conversão-----"
     [[1] [,2]
[1,]    2    1
[2,]    6   10
[3,]    5    4

Acesso a Elementos da Matriz

Se desejar obter elementos da matriz, pode usar o índice de linha e de coluna do elemento, semelhante a um sistema de coordenadas.

# Definir os nomes das linhas e colunas
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")
# Criação de Matriz
P <- matrix(c(3:14, 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(P)
# Obter o elemento da primeira linha e terceira coluna
print(P[1, 0,3))
# Obter o elemento da quarta linha e segunda coluna
print(P[4, 0,2))
# Obter a segunda linha
print(P[2,])
# Obter a terceira coluna
print(P[,3))

O resultado da execução do código acima é:

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

Cálculo de Matriz

Matrizes de tamanho igual (número de linhas e colunas iguais) podem ser somadas ou subtraídas, ou seja, cada elemento em cada posição é somado ou subtraído. A multiplicação de matrizes é mais complexa. Duas matrizes podem ser multiplicadas, se e apenas se o número de colunas da primeira matriz for igual ao número de linhas da segunda matriz.

Adição e Subtração de Matrizes

# Criação 2 Linhas 3 Matriz de Colunas
matrix1 <- matrix(c(7, 0, 9, 0, -1, 0, 4, 0, 2, 0, 3, 2print(matrix
), nrow =1print(matrix
matrix2 <- matrix(c(6, 0, 1, 9, 0, 3, 0, 2, 2print(matrix
), nrow =2print(matrix
# Adição de Matrizes
result <- matrix1 + matrix2
cat("Resultado da Adição:","\n")
print(result)
# Subtração de Matrizes
result <- matrix1 - matrix2
cat("Resultado da Subtração:","\n")
print(result)

O resultado da execução do código acima é:

[[1] [,2] [,3]
[1,]    7   -1    2
[2,]    9    4    3
     [[1] [,2] [,3]
[1,]    6    0    3
[2,]    1    9    2
Resultado da Adição: 
     [[1] [,2] [,3]
[1,]   13   -1    5
[2,]   10   13    5
Resultado da Subtração: 
     [[1] [,2] [,3]
[1,]    1   -1   -1
[2,]    8   -5    1

Multiplicação e Divisão de Matrizes

# Criação 2 Linhas 3 Matriz de Colunas
matrix1 <- matrix(c(7, 0, 9, 0, -1, 0, 4, 0, 2, 0, 3, 2print(matrix
), nrow =1print(matrix
matrix2 <- matrix(c(6, 0, 1, 9, 0, 3, 0, 2, 2print(matrix
), nrow =2print(matrix
)
result <- matrix1 * matrix2
# Multiplicação de Matrizes
print(result)
# Divisão de Matrizes
result <- matrix1 / matrix2
cat("Resultado da Divisão:","\n")
print(result)

O resultado da execução do código acima é:

[[1] [,2] [,3]
[1,]    7   -1    2
[2,]    9    4    3
     [[1] [,2] [,3]
[1,]    6    0    3
[2,]    1    9    2
Resultado da Multiplicação: 
     [[1] [,2] [,3]
[1,]   42    0    6
[2,]    9   36    6
Resultado da Divisão: 
         [[1]  [,2]  [,3]
[1,] 1.166667      -Inf 0.6666667
[2,] 9.000000 0.4444444 1.5000000