Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Matrices

R Matrices

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

The elements of a matrix can be numbers, symbols, or mathematical expressions.

An M x N matrix is a rectangular array of M (row) rows and **N (column) columns** of elements.

The following is a 2-row, 3-column matrix made up of 6 numeric elements:

Matrices in R can be created with the matrix() function; the syntax is as follows:



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

Parameter Description:

Create a numeric matrix:

Example

When byrow is TRUE, elements are arranged by row

M

<-

matrix

(

c

(

3

:

14

)

,

nrow

=

4

, byrow

=

TRUE

)

print

(

M

)

When Ebyrow is FALSE, elements are arranged by column

N

<-

matrix

(

c

(

3

:

14

)

,

nrow

=

4

, byrow

=

FALSE

)

print

(

N

)

Define the names of the 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 of the code above 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

Transposing a matrix

R matrices provide the t() function, which can swap the rows and columns of a matrix.

For example, a matrix with m rows and n columns can be converted to a matrix with n rows and m columns using the t() function.

Example

Create a 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

Convert to a 3-row, 2-column matrix

print

(

t

(

M

)

)

The output of the code above is:



     [,1] [,2] [,3]

[1,]    2    6    5

[2,]    1   10    4

[1] "-----转换-----"

     [,1] [,2]

[1,]    2    1

[2,]    6   10

[3,]    5    4

Accessing matrix elements

To access a matrix element, you can use the element's column index and row index, similar to a coordinate form.

Example

Define the names of the rows and columns

rownames

=

c

(

"row1"

,

"row2"

,

"row3"

,

"row4"

)

colnames

=

c

(

"col1"

,

"col2"

,

"col3"

)

Create a matrix

P

<-

matrix

(

c

(

3

:

14

)

,

nrow

=

4

, byrow

=

TRUE,

dimnames

=

list

(

rownames

,

colnames

)

)

print

(

P

)

Get the element in the first row and third column

print

(

P

[

1

,

3

]

)

Get the element in 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 of the code above 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 calculations

Matrices of the same size (same number of rows and columns) can be added or subtracted, by adding or subtracting the elements at each position. Matrix multiplication is more complex. Two matrices can be multiplied if and only if the number of columns of the first matrix equals the number of rows of the second.

Matrix addition and subtraction

Example

Create a 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

)

Add two matrices

result

<-

matrix1

matrix2

cat

(

"Addition result:"

,

"\n"

)

print

(

result

)

Subtract two matrices

result

<-

matrix1

matrix2

cat

(

"Subtraction result:"

,

"\n"

)

print

(

result

)

The output of the code above is:

[,1] [,2] [,3]

[1,]    7   -1    2

[2,]    9    4    3

     [,1] [,2] [,3]

[1,]    6    0    3

[2,]    1    9    2

Sum 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

Example

Create a 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

)

Multiply two matrices

result

<-

matrix1

matrix2

cat

(

"Multiplication result:"

,

"\n"

)

print

(

result

)

Divide two matrices

result

<-

matrix1

/

matrix2

cat

(

"Division result:"

,

"\n"

)

print

(

result

)

The output of the code above 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