0% found this document useful (0 votes)
3 views

20.1

project on DATA

Uploaded by

DEBASIS BAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

20.1

project on DATA

Uploaded by

DEBASIS BAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab ExpErimEnt 20 : r basics - initiaLizing

matricEs and pErforming matrix opErations


Objective:
To understand the basics of matrices in R, including initialization, basic matrix operations,
and applying functions on matrices.

Part 1: Initializing Matrices in R:


1.1 Creating a Matrix:
In R, a matrix is a two-dimensional array with rows and columns.
# Creating a matrix using matrix() function A <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2, ncol=3, byrow=TRUE)
print(A)
• c(1,2,3,4,5,6): Vector of elements.
• nrow=2: Specifies number of rows.
• ncol=3: Specifies number of columns.
• byrow=TRUE: Fills matrix row-wise.
Output:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

1.2 Creating a Matrix from Vectors:


# Using rbind() and cbind() functions
row1 <- c(1, 2, 3)
row2 <- c(4, 5, 6)
B <- rbind(row1, row2) # Row-wise binding
print(B)
Output:
[,1] [,2] [,3]
row1 1 2 3
row2 4 5 6

col1 <- c(1, 4)


col2 <- c(2, 5)
col3 <- c(3, 6)
C <- cbind(col1, col2, col3) # Column-wise binding
print(C)
Output:
col1 col2 col3

[1,] 1 2 3

[2,] 4 5 6

Part 2: Performing Matrix Operations:


2.1 Basic Matrix Arithmetic:
# Define two matrices
X <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2)
Y <- matrix(c(5, 6, 7, 8), nrow=2, ncol=2)
# Matrix Addition
Z_add <- X + Y
print(Z_add)
Output:
[,1] [,2]

[1,] 6 10

[2,] 8 12

# Matrix Subtraction
Z_sub <- X - Y
print(Z_sub)
Output:
[,1] [,2]

[1,] -4 -4

[2,] -4 -4

2.2 Matrix Multiplication:


# Element-wise multiplication
Z_elem_mult <- X * Y
print(Z_elem_mult)

Output: [,1] [,2]

[1,] 5 21

[2,] 12 32

2.3 Transpose and Inverse of a Matrix:


# Transpose of a Matrix
X_transpose <- t(X)
print(X_transpose)

Output:
[,1] [,2]

[1,] 1 2

[2,] 3 4

# Inverse of a Matrix (only for square and non-singular matrices)


X_inverse <- solve(X) # Assumes X is non-singular
print(X_inverse)

Output: [,1] [,2]

[1,] -2 1

[2,] 1.5 -0.5


2.4 Determinant and Eigenvalues
# Determinant of a Matrix
det_X <- det(X)
print(det_X)

Output:
[1] -2

# Eigenvalues and Eigenvectors


eig_X <- eigen(X)
print(eig_X)

Output:
$values

[1] 5 0

$vectors

[,1] [,2]

[1,] -0.447214 -0.894427

[2,] 0.894427 -0.447214

Conclusion:
This experiment provides hands-on experience with matrices in R, covering initialization, and
basic operations. Understanding these fundamentals is essential for data analysis and
machine learning applications in R.

NAME :- Debasis Bal


Sic :- 24MDSC75
Roll No. :- 26
Dept.:- MSc Data Science

You might also like