Find indices of non zero elements in matrix in R
Last Updated :
18 Apr, 2023
In this article, we will discuss how to find the indices of non-zero elements in Matrix in R Programming Language.
Method 1: Using for loop
A for loop iteration can be performed over the rows and columns to access the cell values contained in a matrix. Every element is checked for non-zero value, and if satisfies the constraint, the corresponding cell indices are displayed. The time complexity required is equivalent to O(n * m), where n is the number of rows and m is the number of columns.
R
# declaring a matrix in R
mat <- matrix(c(-1, 2, 0, 6, 0, 4), nrow = 2)
print ("Original Matrix")
print (mat)
print ("Indices of non-zero elements")
# computing indexes of non
# zero elements looping through
# rows
for (i in 1:nrow(mat)){
# looping through columns
for(j in 1:ncol(mat)){
# check if element is non
# zero
if(mat[i,j]!=0){
# display the row and column
# index
cat(i, j,"\n")
}
}
}
Output
[1] "Original Matrix"
[,1] [,2] [,3]
[1,] -1 0 0
[2,] 2 6 4
[1] "Indices of non-zero elements"
1 1
2 1
2 2
2 3
Method 2: Using which() method
which() method is used to return the position or the index of the value which satisfies the given constraints. It applies the condition over each element of the specified R object, vector or data frame or matrix, and then returns the corresponding cell positions of the satisfied values. Missing values or NA are treated as FALSE in this method.
Syntax: which( cond, arr.ind = FALSE)
Arguments :
cond - can be a logical vector or an array.
arr.ind - logical; indicator of whether the array indexes should be returned.
In case, the arr.ind parameter is set to FALSE, the cell values are returned and not indices. The returned indices are displayed in the form of a table, with row and col as the headings of the respective columns.
R
# declaring a matrix in R
mat <- matrix(c(-1, 2, 0, 6, 0, 4), nrow = 2)
print ("Original Matrix")
print (mat)
print ("Indices of non-zero elements")
# computing indexes of non zero
# elements
which(mat != 0, arr.ind = T)
Output:
[1] "Original Matrix"
[,1] [,2] [,3]
[1,] -1 0 0
[2,] 2 6 4
[1] "Indices of non-zero elements"
row col
[1,] 1 1
[2,] 2 1
[3,] 2 2
[4,] 2 3
The method works in the case of character arrays or matrices as well. In this case, the entire matrix cells' positions are returned as output.
R
# declaring a matrix in R
mat <- matrix(letters[1:8], nrow = 2)
print ("Original Matrix")
print (mat)
print ("Indices of non-zero elements")
# computing indexes of non zero
# elements
which(mat != 0, arr.ind = T)
Output
[1] "Original Matrix"
[,1] [,2] [,3] [,4]
[1,] "a" "c" "e" "g"
[2,] "b" "d" "f" "h"
[1] "Indices of non-zero elements"
row col
[1,] 1 1
[2,] 2 1
[3,] 1 2
[4,] 2 2
[5,] 1 3
[6,] 2 3
[7,] 1 4
[8,] 2 4
Similar Reads
Find the power of a matrix in R In this article, we are going to see how to compute the power of a matrix in R Programming Language. Matrix is an arrangement of numbers into rows and columns. Different ways of finding the power of matrix in R programming: By using %^%.By using a power function. Method 1: By using %^% Before using
2 min read
Set Diagonal of a Matrix to zero in R Matrices are fundamental data structures in R that allow you to store and manipulate two-dimensional data. If you want to set diagonal element of matrix to zero in R programming language then you have to follow these steps to attain it. Concepts Related to the Topic:Matrix Diagonal: In NxN matrix, t
3 min read
Divide Each Row of Matrix by Vector Elements in R In this article, we will discuss how to divide each row of the matrix by vector elements in R Programming Language. Method 1: Using standard division Initially, the transpose of the matrix is computed, to interchange the rows and columns. Initially, if the dimensions of the matrix were n * m , trans
6 min read
Get element at the specific position from matrix in R At any point in time, a matrix may be required to be traversed for an element at a specific position. In this article, we are going to access the elements from a matrix in R Programming Language using integer vector, and logical vector as the index. Accessing Elements in a MatrixIn R Programming Lan
2 min read
How to Set the Diagonal Elements of a Matrix to 1 in R? In this article, we will discuss how to set the diagonal elements of a Matrix to 1 in R Programming Language. Matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and columns are the ones that run vertically. In R program
2 min read
Count non zero values in each column of R dataframe In this article, we are going to count the number of non-zero data entries in the data using R Programming Language. To check the number of non-zero data entries in the data first we have to put that data in the data frame by using: data <- data.frame(x1 = c(1,2,0,100,0,3,10), x2 = c(5,0,1,8,10,0
2 min read
How to Create the Identity Matrix in R? In this article, we will discuss how to create an Identity Matrix in R Programming Language. Identity matrices are the matrices that contain all the zeros, except diagonal elements which are equivalent to 1. The identity matrices are always square in nature. Base R provides a large number of methods
3 min read
Convert matrix to list in R In this article, we will discuss how to convert a given matrix to a List in R Programming Language. Conversion of a matrix into a list in Column-major order The as.list() is an inbuilt function that takes an R language object as an argument and converts the object into a list. We have used this func
2 min read
Select rows of a matrix in R that meet a condition A large dataset is often required to be filtered according to our requirements. In this article, we will be discussing how we can select a row from a matrix in R that meets the condition. For better understanding let's understand the problem statement with the help of an example. Example: Data in us
2 min read
How to Extract Diagonal Elements of a Matrix in R Without Using diag Function? In this article, we will see how to extract diagonal elements of a matrix in R Programming Language without using diag() function. Matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and columns are the ones that run ver
2 min read