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

Matrix

The document provides an overview of matrices, including their creation, indexing, and various functions available in R for matrix manipulation. It explains how to create matrices, access their elements, and perform operations such as multiplication, inversion, and filtering based on conditions. Key functions like is.matrix(), solve(), and cbind() are highlighted, along with examples of filtering matrices based on specific criteria.

Uploaded by

19A540 MAHESH
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Matrix

The document provides an overview of matrices, including their creation, indexing, and various functions available in R for matrix manipulation. It explains how to create matrices, access their elements, and perform operations such as multiplication, inversion, and filtering based on conditions. Key functions like is.matrix(), solve(), and cbind() are highlighted, along with examples of filtering matrices based on specific criteria.

Uploaded by

19A540 MAHESH
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

MATRICES

Matrices

A matrix is a two-dimensional array where each element has the same mode
(numeric, character, or logical). Matrices are created with the matrix function
Matrix Creation

a. vector contains the elements for the matrix


b. nrow and ncol specify the row and column dimensions
c. dimnames contains optional row and column labels stored in character vectors.
d. byrow indicates whether the matrix should be filled in by row (byrow=TRUE)
or by column (byrow=FALSE).
e. The default is by column.
The general format- matrix creation
How to access Elements of a matrix?...Matrix Indexing

Access specific elements of a Matrix

We can access elements of a matrix using the square bracket [] indexing


method. Elements can be accessed as var[row, column]. Here rows and
columns are vectors.
First a 2 x 5 matrix is created containing numbers 1 to 10. By default, the matrix is
filled by column. Then the elements in the 2nd row are selected, followed by the
elements in the 2nd column. Next, the element in the 1st row and 4th column is
selected. Finally, the elements in the 1st row and the 4th and 5th columns are selected.
Functions for Matrices
1. is.matrix() function
2. %*% operator
3. solve() function
4. t() function
5. dim() and dimnames() functions
6. cbind() and rbind() functions
7. diag() function
8. det() function
9. colSums(), rowSums(), and sum() functions
10. colMeans(), rowMeans(), and mean() functions
is.matrix() Function

The is.matrix() function takes an object as input and returns TRUE if the
object is a matrix.
%*% Operator
We can perform the element-wise multiplication of two matrices using the *
operator. But we can do matrix multiplication, with the %*% operator.
solve() Function

The solve() function takes a matrix as input and returns the matrix’s inverse as
output.
t() Function

The t() function in R gives us the transpose of a matrix.


dim() and dimnames() Functions

The dim() function shows the dimension of a matrix. It can also change the dimension of a
matrix and also convert a vector into a matrix by giving it dimensions.

The dimnames() function shows the names of the rows and columns of a matrix. It can also
set or change the names of the rows and columns of a matrix
cbind() and rbind() Functions

The cbind() function joins two or more


matrices or vectors column-wise. The
rbind() function joins them row-wise.
diag() Function

The diag() function can extract or replace the diagonal of a matrix


and can also construct a diagonal matrix
det() Function
The det() function returns the determinant of the input matrix.
colSums(), rowSums(), and sum() Functions
The colSums() function returns the sums of each column of the matrix. The
rowSums() function returns the sum of each row of a matrix. The sum()
function returns the sum of all the elements of the matrix
colMeans(), rowMeans(), and mean() Function
The colMeans() function shows the means of each column of a matrix. The
rowMeans() function shows the means of each row of the matrix. The mean()
function returns the mean of all the elements of the matrix.
Filtering on Matrices

> m<-matrix ( 1:12,nrow=4)


>m
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

Now we filter out the matrix m , based on a condition that the column 2 should be > 6.

> m[m[,2] >= 6,]


[,1] [,2] [,3]
[1,] 2 6 10
[2,] 3 7 11
[3,] 4 8 12
Now we filter out the matrix m, based on a multiple conditions that the column 2 should be > 6 and column 3 > 10.

> m[m[,2] >= 6 & m[,3] > 10,]


[,1] [,2] [,3]
[1,] 3 7 11
[2,] 4 8 12

Note :
The other columns and rows (which are not used in condition)
corresponding to condition column will be return

You might also like