Matrix in R

 

Matrix in R

Introduction

A matrix is a two-dimensional homogeneous data structure in R used to store elements of the same data type arranged in rows and columns.

A matrix is one of the most important data structures in R and is extensively used in:

  • Mathematics
  • Statistics
  • Machine Learning
  • Image Processing
  • Scientific Computing
  • Data Analysis

For example, marks of students in different subjects can be represented as a matrix:

MathsPhysicsChemistry
Student1    859088
Student2928795
Student3788082

Characteristics of Matrices

  • Two-dimensional structure.
  • Homogeneous (all elements have the same type).
  • Elements are arranged in rows and columns.
  • Elements are stored column-wise by default.
  • Support element-wise arithmetic operations.
  • Support matrix operations such as transpose and multiplication.

Creating Matrices

Matrices are created using the matrix() function.

Syntax

matrix(data, nrow, ncol, byrow=FALSE)

where:

  • data : vector containing elements.
  • nrow : number of rows.
  • ncol : number of columns.
  • byrow=FALSE : fills column-wise (default).

Example 1: Column-wise Filling

A <- matrix(c(1,2,3,4,5,6), nrow=2, ncol=3)

print(A)

Output

     [,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6

Example 2: Row-wise Filling

A <- matrix(c(1,2,3,4,5,6),
nrow=2,
ncol=3,
byrow=TRUE)

print(A)

Output

     [,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

Determining Matrix Properties

class()

A <- matrix(1:6,2,3)

class(A)

Output

[1] "matrix"

dim()

Returns dimensions.

dim(A)

Output

[1] 2 3

nrow()

nrow(A)

Output

[1] 2

ncol()

ncol(A)

Output

[1] 3

length()

Returns total number of elements.

length(A)

Output

[1] 6

Accessing Elements

Consider

A <- matrix(1:9,3,3)

Output

     [,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

Access Individual Elements

A[2,3]

Output

[1] 8

(Row 2, Column 3)


Access a Row

A[2,]

Output

[1] 2 5 8

Access a Column

A[,2]

Output

[1] 4 5 6

Access Multiple Rows and Columns

A[1:2,2:3]

Output

     [,1] [,2]
[1,] 4 7
[2,] 5 8

Naming Rows and Columns

A <- matrix(1:9,3,3)

rownames(A) <- c("R1","R2","R3")

colnames(A) <- c("C1","C2","C3")

print(A)

Output

   C1 C2 C3
R1 1 4 7
R2 2 5 8
R3 3 6 9

Matrix Arithmetic

Consider

A <- matrix(c(1,2,3,4),2,2)
B <- matrix(c(5,6,7,8),2,2)

Addition

A+B

Output

     [,1] [,2]
[1,] 6 10
[2,] 8 12

Subtraction

A-B

Element-wise Multiplication

A*B

Output

     [,1] [,2]
[1,] 5 21
[2,] 12 32

Scalar Multiplication

2*A

Output

     [,1] [,2]
[1,] 2 6
[2,] 4 8

Matrix Multiplication

Unlike *, matrix multiplication uses %*%.

A <- matrix(c(1,2,3,4),2,2)
B <- matrix(c(5,6,7,8),2,2)

A %*% B

Output

     [,1] [,2]
[1,] 23 31
[2,] 34 46



Transpose of a Matrix

The transpose interchanges rows and columns.

A <- matrix(c(1,2,3,4,5,6),2,3)

t(A)

Output

     [,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6

Diagonal Matrix

diag(c(1,2,3))

Output

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

Identity Matrix

diag(3)

Output

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

Row and Column Sums

A <- matrix(1:9,3,3)

rowSums(A)

Output

[1] 12 15 18
colSums(A)

Output

[1]  6 15 24

Mean, Maximum and Minimum

mean(A)
max(A)
min(A)

Output

5
9
1

Applying Functions

Row-wise Maximum

apply(A,1,max)

Output

[1] 7 8 9

Column-wise Sum

apply(A,2,sum)

Output

[1]  6 15 24

Binding Matrices

Row Binding

A <- matrix(1:4,2,2)
B <- matrix(5:8,2,2)

rbind(A,B)

Output

     [,1] [,2]
[1,] 1 3
[2,] 2 4
[3,] 5 7
[4,] 6 8

Column Binding

cbind(A,B)

Output

     [,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8

Matrix Inverse

A <- matrix(c(1,2,3,4),2,2)

solve(A)

Output

     [,1] [,2]
[1,] -2 1.0
[2,] 1 -0.5

Determinant

det(A)

Output

[1] -2

Eigenvalues and Eigenvectors

eigen(A)

Output

$values
...

$vectors
...

Converting Vector to Matrix

v <- 1:9

A <- matrix(v,3,3)

print(A)

Difference Between Vector, Matrix and Array

StructureDimensions
Vector        1D
Matrix        2D
Array        Multi-dimensional

Applications of Matrices

Marks of Students

Rows → Students
Columns → Subjects

Image Processing

Rows → Height
Columns → Width

Machine Learning

Feature matrices

Linear Algebra

Systems of equations

Graph Theory

Adjacency matrices

Statistics

Correlation matrices


Important Matrix Functions

FunctionPurpose
matrix()        Create matrix
dim()        Dimensions
nrow()        Number of rows
ncol()        Number of columns
t()        Transpose
%*%        Matrix multiplication
diag()        Diagonal/Identity matrix
det()        Determinant
solve()        Inverse
eigen()        Eigenvalues and eigenvectors
rowSums()        Sum of rows
colSums()        Sum of columns
apply()        Apply functions row/column-wise

Conclusion

A matrix is a two-dimensional homogeneous data structure used to represent tabular and mathematical data. Matrices support powerful operations such as addition, multiplication, transpose, inverse, and statistical computations, making them indispensable in mathematics, statistics, machine learning, and scientific computing.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1