# Matrices en RStudio

El comando que permite introducir matrices en RStudio es matrix().
Para construir una matriz B escribimos: B <−matrix(c(); ncol =; nrow =)
Donde 𝑐() corresponde al vector de las entradas de la matriz A separadas por comas y siguiendo el orden de las columnas, además 𝑛𝑐𝑜𝑙 corresponde al número de columnas y 𝑛𝑟𝑜𝑤 el número de filas.
Ejercicio 1: Considerando la siguiente matriz

\[ A=\begin{bmatrix} 1 & 2 & 3 \\ 2 & 4 & 6 \\ 3 & 6 & 9 \\ 4 & 8 & 12 \\ \end{bmatrix} \]

Código del ejercicio:
Crearemos una matriz y usaremos la siguiente función:
a<-c(1,2,3,4,2,4,6,8,3,6,9,10)
b<- matrix(a,ncol = 3)
b
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    2    4    6
## [3,]    3    6    9
## [4,]    4    8   10
Ejercicio 2: Introducir la matriz identidad de tamaño 4x4 en RStudio (sin usar un vector de 16 valores)

\[ I=\begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1\\ \end{bmatrix} \]

Código del ejercicio:
Crearemos una matriz y usaremos la siguiente función:
j<-diag(1,4,4)
j
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
Ejercicio 3: Encontrar la matriz inversa de L, donde L se define como:

\[ L=\begin{bmatrix} 1 & 2 & -4 \\ -1 & -1 & 5 \\ 2 & 7 & -3 \\ \end{bmatrix} \]

Código del ejercicio:
Crearemos una matriz, usarems vectores y la siguiente función:
c1<-c(1,-1,2)
c2<-c(2,-1,7)
c3<-c(-4,5,-3)
ind<-cbind(c1,c2,c3)
ind
##      c1 c2 c3
## [1,]  1  2 -4
## [2,] -1 -1  5
## [3,]  2  7 -3
Ejercicio 4: Suponga que se quiere ingresar una matriz con muchas entradas como la matriz P que se presenta a continuación.

\[ P=\begin{bmatrix} 1 & 2 & 3 & 0 & 2 \\ 2 & 4 & 6 & 0 & 3 \\ 3 & 6 & 9 & 0 & 5 \\ 4 & 8 & 12 & 0 & 7 \\ 5 & 10 & 15 & 5 & 11 \\ 6 & 12 & 18 & 5 & 13 \\ 7 & 14 & 21 & 5 & 17 \\ 8 & 16 & 24 & 5 & 19 \\ 9 & 18 & 27 & 5 & 23 \\ \end{bmatrix} \]

Código del ejercicio:
Crearemos un documento en excel con los valores de la matriz y usaremos el siguiente package:
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.1
P <- read_excel("Ejercicio4.xlsx")
## New names:
## • `2` -> `2...2`
## • `2` -> `2...5`
P
## # A tibble: 8 × 5
##     `1` `2...2`   `3`   `0` `2...5`
##   <dbl>   <dbl> <dbl> <dbl>   <dbl>
## 1     2       4     6     0       3
## 2     3       6     9     0       5
## 3     4       8    12     0       7
## 4     5      10    15     5      11
## 5     6      12    18     5      13
## 6     7      14    21     5      17
## 7     8      16    24     5      19
## 8     9      18    27     5      23
Ejercicio 5: Resolver el siguiente sistema de ecuaciones usando R:

\[ \begin{bmatrix} x + 5y = 7\\ -2x-7y=-5\\ \end{bmatrix} \]

Código del ejercicio:
Crearemos una matriz y usaremos la siguiente función:
coe <- matrix(c(1, 5, -2, -7), nrow = 2, byrow = T) 
cons <- c(7, -5) 
resultado<-solve(coe,cons)
resultado
## [1] -8  3
Ejercicio 6: Realice el determinate de la siguiente matriz, la solución manual se adjunta, usted debe realizarlo por R, puede usar la funcion det y comprobar los resultados.

\[ A=\begin{bmatrix} 1 & 4 & 9\\ 7 & 2 & 5\\ 6 & 8 & 3\\ \end{bmatrix} \]

\[ |A|= (1*2*3)+(4*5*6)+(7*8*9)-(9*2*6)-(4*7*3)-(5*8*1) \]

\[ |A|= 6+120+504-108-84-40 \]

\[ |A|= 398 \]

Código del ejercicio:
Crearemos una matriz y usaremos la siguiente función para saber el determinante:
m1<-matrix(c(1,4,9,7,2,5,6,8,3),nrow=3,byrow = T)
m1
##      [,1] [,2] [,3]
## [1,]    1    4    9
## [2,]    7    2    5
## [3,]    6    8    3
det(m1)
## [1] 398
Ejercicio 7: Realizar en R la transpuesta de la matriz propuesta a continuación:

\[ A=\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9\\ \end{bmatrix} \]

\[ A^{T}=\begin{bmatrix} 1 & 4 & 7\\ 2 & 5 & 8\\ 3 & 6 & 9\\ \end{bmatrix} \]

Código del ejercicio:
Crearemos vectores, luego crearemos una matriz y usaremos la siguiente función para su transpuesta:
A1 <- c(1,4,7)
A2 <- c(2,5,8)
A3 <- c(3,6,9)
MatrizA <- cbind(A1,A2,A3)
MatrizA
##      A1 A2 A3
## [1,]  1  2  3
## [2,]  4  5  6
## [3,]  7  8  9
ATranspuesta=cbind(A1,A2,A3)
t(ATranspuesta)
##    [,1] [,2] [,3]
## A1    1    4    7
## A2    2    5    8
## A3    3    6    9