Open In App

How to find length of matrix in R

Last Updated : 25 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will examine various methods to find the length of a matrix by using R Programming Language.

What is a matrix?

A matrix is a two-dimensional data structure that is a collection of rows and columns. A matrix can able to contain data of various types such as numeric, characters, and logical values. It is possible to access the data in the matrix easily. By using the function ‘matrix()’ matrices are created.

How to find the length of the matrix

R language offers various methods to find the length of the matrix efficiently. By using these methods provided by R, it is possible to find the length of the matrix easily. Some of the methods to find the length of the matrix are:

By using the function ‘length()’

These method is used to find the length of the matrix. The syntax is:

length(matrix)

In the below example, we created a matrix and find the length of the matrix by using the function ‘length()’.

R
#vector
a=c(21:56)

  #Creating matrix
mat <- matrix(a, nrow = 6, ncol = 6)
print(mat)

# Finding the length of matrix 
len_mat <- length(mat)
print("The length of the matrix is")
print(len_mat)

Output:

       [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   21   27   33   39   45   51
[2,]   22   28   34   40   46   52
[3,]   23   29   35   41   47   53
[4,]   24   30   36   42   48   54
[5,]   25   31   37   43   49   55
[6,]   26   32   38   44   50   56

[1] "The length of the matrix is"
[1] 36

In the below example, we created a matrix and find the length of the matrix by using the function ‘length()’.

R
# character vector
 x=letters[3:26]
 
  #Creating matrix
matrix = matrix(x, nrow = 4, ncol = 6)
print(matrix)

# Finding the length of matrix 
len_mat = length(matrix)
print("The length of the matrix is")
print(len_mat)

Output:

      [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "c"  "g"  "k"  "o"  "s"  "w" 
[2,] "d"  "h"  "l"  "p"  "t"  "x" 
[3,] "e"  "i"  "m"  "q"  "u"  "y" 
[4,] "f"  "j"  "n"  "r"  "v"  "z" 

[1] "The length of the matrix is"
[1] 24

Finding the length of matrix by using ‘nrow()’ and ‘ncol()’ functions

The function nrow() is used to find the number of rows are present in the matrix. similarly, ncol() is used to find the number of columns are present in the matrix. By multiplying the number of rows and columns, can able to find the length of the matrix efficiently. In the below example, we created a matrix and find the length of the matrix.

R
# Creating the matrix
matrix <- matrix(11:30, nrow = 5, ncol =4)
print(matrix)

# Finding number of rows
n_rows <- nrow(matrix)

#Finding number of columns
n_cols <- ncol(matrix)

# Calculating the length of the matrix
matrix_length <- n_rows * n_cols
print("The length of the matrix is")
print(matrix_length)

Output:

      [,1] [,2] [,3] [,4]
[1,]   11   16   21   26
[2,]   12   17   22   27
[3,]   13   18   23   28
[4,]   14   19   24   29
[5,]   15   20   25   30

[1] "The length of the matrix is"
[1] 20

In the below example, we created a matrix and find the length of the matrix.

R
# Creating the matrix
matrix = matrix(rpois(60,5), ncol=4)
print(matrix)

# Finding number of rows
n_rows = nrow(matrix)

#Finding number of columns
n_cols = ncol(matrix)

# Calculating the length of the matrix
matrix_length = n_rows * n_cols
print("The length of the matrix is")
print(matrix_length)

Output:

   [,1] [,2] [,3] [,4]
 [1,]    3    3    4    5
 [2,]    1    3    4    4
 [3,]    7    1    3   10
 [4,]    3    6    3    5
 [5,]    3    4    7    5
 [6,]    6    4    6    7
 [7,]    4    3    5    3
 [8,]    3    4    3    6
 [9,]    9    2    7    5
[10,]    6    6    5    6
[11,]    1    1    6    4
[12,]    2    8    1    1
[13,]    5    7    4    4
[14,]    8    5    7    8
[15,]    5    6    1    5

[1] "The length of the matrix is"
[1] 60

Conclusion

In Conclusion, we learned about how to find the length of the matrix using R. R language offers versatile tools while finding the length of the matrix efficiently.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads