## What are Matrices?

Matrices are the R objects wherein the elements are organized in a 2-D rectangular shape. In a matrix, it contains elements of the same atomic types.

Matrix function is denoted as a matrix().

### Syntax

`matrix(data, nrow, ncol, byrow, dimnames)`
• data is the parameter of input,
• nrow is number of rows and
• ncol is the number of columns to be created;
• byrow has TRUE or FALSE as its logical values, and dimname is the rows or columns name.

Get familiar with the top R Interview Questions to get a head start in your career!

### Accessing Elements of a matrix

Any row or column element can be accessed by using the column and row index.

Want to get certified in R Programming! Learn R from top R Programming experts and excel in your career with Intellipaat’s Data Science with R Certification!

Example:

```#Defining the column and row names
rn = c("r1", "r2", "r3", r4")
cn = c("c1", "c2", "c3")
#Creating the matrix
A <- matrix(c(1:12), nrow = 4, byrow = TRUE, dimnames= list(rn, cn))
#Accessing 2nd row, 3rd column element
print(A[2,3])
#Accessing Â elements of 4th column
print(A[ ,4])```

Output:

```[1] 6Â
row1Â  row2Â  row3Â  row4
Â Â  3 Â Â Â Â Â Â  6Â Â Â Â Â Â Â Â Â  9Â Â Â Â Â Â Â  12```

Have you got more queries? Come to our R Programming Community and get them clarified today!

### Matrix Computations

Different types of mathematical operations such as matrix addition, subtraction, multiplication, and division can be performed on the matrices using R Programming operators.

Recommended Videos

Course Schedule

Name Date
Data Science Course 2021-06-26 2021-06-27
(Sat-Sun) Weekend batch
View Details
Data Science Course 2021-07-03 2021-07-04
(Sat-Sun) Weekend batch
View Details
Data Science Course 2021-07-10 2021-07-11
(Sat-Sun) Weekend batch
View Details

Browse Categories