How to Find Index of Element in Array in MATLAB?

In a cluster, components are set on specific records beginning from 1, etc. To observe the record of a worth in a given cluster, we can utilize the find() work. The find() work is utilized to track down lists and upsides of components in an exhibit or lattice To observe lists of components, we really want to characterize a condition inside the find() work. For instance, to observe the record of a solitary component, we can utilize the equivalent administrator inside the find() work. Assuming a similar component is available at various lists, the find() capacity will return all the files where the component is available. We can utilize a wide range of restrictive explanations inside this capacity. For instance, assume we need to find records of the relative multitude of components more prominent than a specific number. All things considered, we can utilize the more prominent than administrator, and it will return the files of the multitude of components more noteworthy than that particular number.

How to Find Index of Element in Array in MATLAB?

  • find(X) : Return a vector containing the indices of elements
  • find(X,n): Return first n indices of the elements in X
  • find(X,n, Direction): find n indices in X according to the Direction  where Direction – ‘first‘ or ‘last
  • [row,col] = find(): It returns the row and column subscript of element in array
  • [row,col,V] = find(): returns vector V containing non-zero elements

For instance, how about we observe the list of a solitary component present inside a given cluster. See the code beneath.

mat = [2 3 1 2];
indices = find(mat==2)

Output:

indices =

     1     4

The variable files contain two qualities in the above yield, which implies the component is available at file 1 and 4 inside the cluster or vector. Presently how about we consider, we need to observe the records of components that are more noteworthy than 1. We really want to change the condition inside the find() work. Rather than utilizing the equivalent administrator, we will utilize the more noteworthy than administrator. See the code beneath.

mat = [2 3 1 2];
indices = find(mat>1)

Output:

indices =

     1     2     4

In the above yield, the variable files contain three qualities, and as may be obvious, three components are more noteworthy than one inside the given exhibit or vector. To observe a component’s situation inside the vector, we can utilize the find() work. We realize that the component inside a lattice is set on a specific line and segment, and to see that particular line and segment, we can utilize the find() work. Assuming that the component is available at various positions, the find() capacity will return numerous qualities for line and segment. See the code beneath.

mat = [2 3 ; 2 1]
[row, col] = find(mat==2)

Output:

mat =

     2     3
     2     1


row =

     1
     2


col =

     1
     1

In the above yield, the framework has two lines and two sections, the primary worth of the line and segment vector is the principal position of the component, which is first line and first segment, and the second worth of the line and segment is the second place of the component which is second line and first segment. To track down the records of component 2 in the given lattice, the observe capacity will return 1 and 2 on the grounds that the qualities are put segment savvy on account of the network. That implies the worth 2 is at file 1 and 2 and worth 3 is at file 3, and worth 1 is at list 4. So if rather than two result factors, you just pass one variable, the find() capacity will return the files section shrewd. For instance, how about we track down the lists of the component and supplant them with another worth. See the code beneath.

mat = [2 3 ; 2 1]
indices = find(mat==2)
mat(indices) = 5

Output:


mat =

     2     3
     2     1


indices =

     1
     2


mat =

     5     3
     5     1

In the above code, first, we observe the files of component two and afterward supplant the worth with 5 utilizing the lists, and as may be obvious, the framework esteems have been changed. To utilize the find() work for reasons unknown, you can generally make your capacity utilizing a for circle and if explanation. You really want to cross every one of the components of the exhibit or framework, and utilizing the if articulation, you can check assuming the current worth matches your ideal worth or not. Assuming it is coordinated with your ideal worth, you can save its record and continue on until you have checked every one of the components present inside the cluster. For instance, how about we utilize a for circle and an if proclamation to track down the files of a vector or cluster See the code beneath.

mat = [2 3 2 1]
indices = [];
for i=1:length(mat)
    if(mat(i) == 2)
        indices = [indices i];
    end
end
indices

Output:

mat =

     2     3     2     1


indices =

     1     3

In the above code, we introduced the variable records with an unfilled vector. At the point when the worth two coordinates with any component inside the exhibit, we will save that file in the records variable.

Also ReadHow to convert Dictionary to Pandas Dataframe?

Leave a Reply

Your email address will not be published.