Related Articles

# Find indices of non zero elements in matrix in R

• Last Updated : 17 May, 2021

In this article, we will discuss how to find the indices of non-zero elements in Matrix in R Programing 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```

My Personal Notes arrow_drop_up