Find indices and values of nonzero elements (2024)

Find indices and values of nonzero elements

collapse all in page

Syntax

k = find(X)

k = find(X,n)

k = find(X,n,direction)

[row,col]= find(___)

[row,col,v]= find(___)

Description

example

k = find(X) returns a vector containing the linear indices of each nonzero element in array X.

  • If X is a vector, then find returns a vector with the same orientation as X.

  • If X is a multidimensional array, then find returns a column vector of the linear indices of the result.

example

k = find(X,n) returnsthe first n indices corresponding to the nonzeroelements in X.

example

k = find(X,n,direction),where direction is 'last', findsthe last n indices corresponding to nonzero elementsin X. The default for direction is 'first',which finds the first n indices corresponding tononzero elements.

example

[row,col]= find(___) returns the row and column subscriptsof each nonzero element in array X using any ofthe input arguments in previous syntaxes.

example

[row,col,v]= find(___) also returns vector v,which contains the nonzero elements of X.

Examples

collapse all

Zero and Nonzero Elements in Matrix

Open Live Script

Find the nonzero elements in a 3-by-3 matrix.

X = [1 0 2; 0 1 1; 0 0 4]
X = 3×3 1 0 2 0 1 1 0 0 4
k = find(X)
k = 5×1 1 5 7 8 9

Use the logical not operator on X to locate the zeros.

k2 = find(~X)
k2 = 4×1 2 3 4 6

Elements Satisfying a Condition

Open Live Script

Find the first five elements that are less than 10 in a 4-by-4 magic square matrix.

X = magic(4)
X = 4×4 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
k = 5×1 2 3 4 5 7

View the corresponding elements of X.

X(k)
ans = 5×1 5 9 4 2 7

Elements Equal to Specific Values

Open Live Script

To find a specific integer value, use the == operator. For instance, find the element equal to 13 in a 1-by-10 vector of odd integers.

x = 1:2:20
x = 1×10 1 3 5 7 9 11 13 15 17 19
k = find(x==13)
k = 7

To find a noninteger value, use a tolerance value based on your data. Otherwise, the result is sometimes an empty matrix due to floating-point roundoff error.

y = 0:0.1:1
y = 1×11 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
k = find(y==0.3)
k = 1x0 empty double row vector
k = find(abs(y-0.3) < 0.001)
k = 4

Last Several Nonzero Elements

Open Live Script

Create a 6-by-6 magic square matrix with all of the odd-indexed elements equal to zero.

X = magic(6);X(1:2:end) = 0
X = 6×6 0 0 0 0 0 0 3 32 7 21 23 25 0 0 0 0 0 0 8 28 33 17 10 15 0 0 0 0 0 0 4 36 29 13 18 11

Locate the last four nonzeros.

k = find(X,4,'last')
k = 4×1 30 32 34 36

Elements Satisfying Multiple Conditions

Open Live Script

Find the first three elements in a 4-by-4 matrix that are greater than 0 and less than 10. Specify two outputs to return the row and column subscripts to the elements.

X = [18 3 1 11; 8 10 11 3; 9 14 6 1; 4 3 15 21]
X = 4×4 18 3 1 11 8 10 11 3 9 14 6 1 4 3 15 21
[row,col] = find(X>0 & X<10,3)
row = 3×1 2 3 4
col = 3×1 1 1 1

The first instance is X(2,1), which is 8.

Subscripts and Values for Nonzero Elements

Open Live Script

Find the nonzero elements in a 3-by-3 matrix. Specify three outputs to return the row subscripts, column subscripts, and element values.

X = [3 2 0; -5 0 7; 0 0 1]
X = 3×3 3 2 0 -5 0 7 0 0 1
[row,col,v] = find(X)
row = 5×1 1 2 1 2 3
col = 5×1 1 1 2 3 3
v = 5×1 3 -5 2 7 1

Subscripts of Multidimensional Array

Open Live Script

Find the nonzero elements in a 4-by-2-by-3 array. Specify two outputs, row and col, to return the row and column subscripts of the nonzero elements. When the input is a multidimensional array (N > 2), find returns col as a linear index over the N-1 trailing dimensions of X.

X = zeros(4,2,3);X([1 12 19 21]) = 1
X = X(:,:,1) = 1 0 0 0 0 0 0 0X(:,:,2) = 0 0 0 0 0 0 1 0X(:,:,3) = 0 1 0 0 1 0 0 0
[row,col] = find(X)
row = 4×1 1 4 3 1
col = 4×1 1 3 5 6

Input Arguments

collapse all

XInput array
scalar | vector | matrix | multidimensional array

Input array, specified as a scalar, vector, matrix, or multidimensional array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
Complex Number Support: Yes

nNumber of nonzeros to find
positive integer scalar

Number of nonzeros to find, specified as a positive integerscalar. By default, find(X,n) looks for the first n nonzeroelements in X.

directionSearch direction
'first' (default) | 'last'

Search direction, specified as the string 'first' or 'last'.Look for the last n nonzeroelements in X using find(X,n,'last').

Output Arguments

collapse all

k — Indices to nonzero elements
vector

Indices to nonzero elements, returned as a vector.

  • If X is a row vector, then k is also a row vector. Otherwise, k is a column vector.

  • k is an empty row vector or empty column vector when X is an empty array or has no nonzero elements.

  • find uses the convention that k is an empty matrix [] when X is an empty matrix [].

You can return the nonzero values in X using X(k).

row — Row subscripts
vector

Row subscripts, returned as a vector. Together, row and col specifythe X(row,col) subscripts corresponding to thenonzero elements in X.

col — Column subscripts
vector

Column subscripts, returned as a vector. Together, row and col specifythe X(row,col) subscripts corresponding to thenonzero elements in X.

If X is a multidimensional array with N> 2, then col is a linear index overthe N-1 trailing dimensions of X.This preserves the relation X(row(i),col(i)) == v(i).

v — Nonzero elements of X
vector

Nonzero elements of X, returned as a vector.

More About

collapse all

Linear Indices

A linear index allows use of a single subscriptto index into an array, such as A(k). MATLAB® treatsthe array as a single column vector with each column appended to thebottom of the previous column. Thus, linear indexing numbers the elementsin the columns from top to bottom, left to right.

For example, consider a 3-by-3 matrix. You can reference the A(2,2) elementwith A(5), and the A(2,3) elementwith A(8). The linear index changes depending onthe size of the array; A(5) returns a differentlylocated element for a 3-by-3 matrix than it does for a 4-by-4 matrix.

The sub2ind and ind2sub functionsare useful in converting between subscripts and linear indices.

Tips

  • To find array elements that meet a condition, use find inconjunction with a relational expression. For example, find(X<5) returnsthe linear indices to the elements in X that areless than 5.

  • To directly find the elements in X thatsatisfy the condition X<5, use X(X<5).Avoid function calls like X(find(X<5)), whichunnecessarily use find on a logical matrix.

  • When you execute find with a relationaloperation like X>1, it is important to rememberthat the result of the relational operation is a logical matrix ofones and zeros. For example, the command [row,col,v] = find(X>1) returnsa column vector of logical 1 (true)values for v.

  • The row and column subscripts, row and col,are related to the linear indices in k by k= sub2ind(size(X),row,col).

Extended Capabilities

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

See Also

ind2sub | nonzeros | strfind | sub2ind | Short-Circuit AND | Short-Circuit OR | ismember

Topics

  • Find Array Elements That Meet a Condition
  • Array Indexing
  • Relational Operations
  • Sparse Matrices

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Find indices and values of nonzero elements (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Find indices and values of nonzero elements (2024)

FAQs

How do you find the indices of non zero elements in Python? ›

Get Indices of nonzero Elements Use nonzero()

nonzero() function. Here, arr2 is a tuple containing one array, which represents the indices of the non-zero elements in the original array arr . The output indicates that non-zero elements are at positions 0, 1, 2, 4, 6, and 8 in the array.

What is NumPy indices of nonzero values? ›

nonzero() function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values in the array can be obtained with arr[nonzero(arr)] .

How do you find the index of a nonzero element in MATLAB? ›

Description. k = find( X ) returns a vector containing the linear indices of each nonzero element in array X . If X is a vector, then find returns a vector with the same orientation as X . If X is a multidimensional array, then find returns a column vector of the linear indices of the result.

How to find the number of non-zero elements in MATLAB? ›

N = nnz( X ) returns the number of nonzero elements in matrix X .

How do you count non-zero entries in Python? ›

  1. To count the number of non-zeros of an entire dataframe, np.count_nonzero(df)
  2. To count the number of non-zeros of all rows np.count_nonzero(df, axis=0)
  3. To count the number of non-zeros of all columns np.count_nonzero(df, axis=1)
Sep 26, 2014

How do you find indices in Python? ›

Python's built-in index() function is a useful tool for finding the index of a specific element in a sequence. This function takes an argument representing the value to search for and returns the index of the first occurrence of that value in the sequence.

What is a non-zero element? ›

In mathematics, a non-zero element is any element of an algebraic structure other than the zero element. Nonzero: The Logic of Human Destiny, 1999 book by Robert Wright. Nonzero Records, independent record label based in Sydney, Australia.

How do you check an array for non-zero values? ›

B = any( A ) tests along the first array dimension of A whose size does not equal 1, and determines if any element is a nonzero number or logical 1 ( true ). In practice, any is a natural extension of the logical OR operator.

How do you find no index? ›

To test if your noindex implementation is correct, use the URL Inspection tool to see the HTML that Googlebot received while crawling the page. You can also use the Page Indexing report in Search Console to monitor the pages on your site from which Googlebot extracted a noindex rule.

How do you count the number of non-zero elements in an array? ›

count_nonzero(x)): The np. count_nonzero() function is used to count the number of non-zero elements in the array 'x'. In this case, there are 5 non-zero elements in the array.

How do you find the indices of values in an array in MATLAB? ›

In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indices and the element from the array. The find() function returns a vector containing the data.

How to check if a list contains a non-zero value in Python? ›

Method 4: Using filter() function
  1. Define a function that takes an element and its index as input and returns True if the element is non-zero, else False.
  2. Apply the filter() function on the enumerated test_list with the above-defined function.
May 9, 2023

What is the non-zero value in Python? ›

The nonzero() function in Python is used to return the indices (the index numbers or index positions) of the elements of an array that are non-zero.

How to get all indices of an element in a string in Python? ›

5 Ways to Find the Index of a Substring in Python
  1. str.find()
  2. str.rfind()
  3. str.index()
  4. str.rindex()
  5. re.search()

References

Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 5773

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.