Find Array Elements That Meet a Condition - MATLAB & Simulink (2024)

Open Live Script

This example shows how to filter the elements of an array by applying conditions to the array. For instance, you can examine the even elements in a matrix, find the location of all 0s in a multidimensional array, or replace NaN values in data. You can perform these tasks using a combination of the relational and logical operators. The relational operators (>, <, >=, <=, ==, ~=) impose conditions on the array, and you can apply multiple conditions by connecting them with the logical operators and, or, and not, respectively denoted by the symbols &, |, and ~.

Apply a Single Condition

To apply a single condition, start by creating a 5-by-5 matrix that contains random integers between 1 and 15. Reset the random number generator to the default state for reproducibility.

rng defaultA = randi(15,5)
A = 5×5 13 2 3 3 10 14 5 15 7 1 2 9 15 14 13 14 15 8 12 15 10 15 13 15 11

Use the relational less than operator, <, to determine which elements of A are less than 9. Store the result in B.

B = A < 9
B = 5x5 logical array 0 1 1 1 0 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0

The result is a logical matrix. Each value in B represents a logical 1 (true) or logical 0 (false) state to indicate whether the corresponding element of A fulfills the condition A < 9. For example, A(1,1) is 13, so B(1,1) is logical 0 (false). However, A(1,2) is 2, so B(1,2) is logical 1 (true).

Although B contains information about which elements in A are less than 9, it doesn’t tell you what their values are. Rather than comparing the two matrices element by element, you can use B to index into A.

ans = 8×1 2 2 5 3 8 3 7 1

The result is a column vector of the elements in A that are less than 9. Since B is a logical matrix, this operation is called logical indexing. In this case, the logical array being used as an index is the same size as the other array, but this is not a requirement. For more information, see Array Indexing.

Some problems require information about the locations of the array elements that meet a condition rather than their actual values. In this example, you can use the find function to locate all of the elements in A less than 9.

I = find(A < 9)
I = 8×1 3 6 7 11 14 16 17 22

The result is a column vector of linear indices. Each index describes the location of an element in A that is less than 9, so in practice A(I) returns the same result as A(B). The difference is that A(B) uses logical indexing, whereas A(I) uses linear indexing.

Apply Multiple Conditions

You can use the logical and, or, and not operators to apply any number of conditions to an array; the number of conditions is not limited to one or two.

First, use the logical and operator, denoted &, to specify two conditions: the elements must be less than 9 and greater than 2. Specify the conditions as a logical index to view the elements that satisfy both conditions.

A(A<9 & A>2)
ans = 5×1 5 3 8 3 7

The result is a list of the elements in A that satisfy both conditions. Be sure to specify each condition with a separate statement connected by a logical operator. For example, you cannot specify the conditions above by A(2<A<9), since it evaluates to A(2<A | A<9).

Next, find the elements in A that are less than 9 and even numbered.

A(A<9 & ~mod(A,2))
ans = 3×1 2 2 8

The result is a list of all even elements in A that are less than 9. The use of the logical NOT operator, ~, converts the matrix mod(A,2) into a logical matrix, with a value of logical 1 (true) located where an element is evenly divisible by 2.

Finally, find the elements in A that are less than 9 and even numbered and not equal to 2.

A(A<9 & ~mod(A,2) & A~=2)
ans = 8

The result, 8, is even, less than 9, and not equal to 2. It is the only element in A that satisfies all three conditions.

Use the find function to get the index of the element equal to 8 that satisfies the conditions.

find(A<9 & ~mod(A,2) & A~=2)
ans = 14

The result indicates that A(14) = 8.

Replace Values That Meet a Condition

Sometimes it is useful to simultaneously change the values of several existing array elements. Use logical indexing with a simple assignment statement to replace the values in an array that meet a condition.

Replace all values in A that are greater than 10 with the number 10.

A(A>10) = 10
A = 5×5 10 2 3 3 10 10 5 10 7 1 2 9 10 10 10 10 10 8 10 10 10 10 10 10 10

Next, replace all values in A that are not equal to 10 with a NaN value.

A(A~=10) = NaN
A = 5×5 10 NaN NaN NaN 10 10 NaN 10 NaN NaN NaN NaN 10 10 10 10 10 NaN 10 10 10 10 10 10 10

Lastly, replace all of the NaN values in A with zeros and apply the logical NOT operator, ~A.

A(isnan(A)) = 0;C = ~A
C = 5x5 logical array 0 1 1 1 0 0 1 0 1 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0

The resulting matrix has values of logical 1 (true) in place of the NaN values, and logical 0 (false) in place of the 10s. The logical NOT operation, ~A, converts the numeric array into a logical array such that A&C returns a matrix of logical 0 (false) values and A|C returns a matrix of logical 1 (true) values.

See Also

nan | Short-Circuit AND | Short-Circuit OR | isnan | find | and | or | xor | not

Find Array Elements That Meet a Condition
- MATLAB & Simulink (2024)

FAQs

How do I find a specific element 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 do I access array elements in Simulink? ›

Always specify the row first and column second. To refer to multiple elements of an array, use the colon ':' operator, which allows you to specify a range of elements using the form 'start:end'. The colon alone, without start or end values, specifies all the elements in that dimension.

How do you access individual elements of an array in MATLAB? ›

When you want to access selected elements of an array, use indexing. Using a single subscript to refer to a particular element in an array is called linear indexing. If you try to refer to elements outside an array on the right side of an assignment statement, MATLAB throws an error.

How to find the number of elements in an array in MATLAB? ›

n = numel( A ) returns the number of elements, n , in array A , equivalent to prod(size(A)) .

How do you search for a specific value in an array? ›

If you need to find if a value exists in an array, use includes() . Again, it checks each element for equality with the value instead of using a testing function. If you need to find if any element satisfies the provided testing function, use some() .

How do I find a specific element in an array? ›

Different ways to search an item in an array in JavaScript
  1. Using find() method.
  2. Using findIndex() method.
  3. Using includes() method.
  4. Using some() method.
  5. Using indexOf() method.
  6. Using filter() method.
  7. Using every() method.
Jun 4, 2024

How do you access the elements of an array? ›

You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

How to access array data in MATLAB? ›

If a cell contains an array, you can access specific elements within that array using two levels of indices. First, use curly braces to access the contents of the cell. Then, use the standard indexing syntax for the type of array in that cell. For example, C{2,3} returns a 3-by-3 matrix of random numbers.

How to access the first element of an array in MATLAB? ›

The most common way is to explicitly specify the indices of the elements. For example, to access a single element of a matrix, specify the row number followed by the column number of the element.

How do you access a specific part of an array? ›

Array index starts with 0 to represent the first element of an array, 1 for the second element and so on. For example, given an array ["apple", "banana", "orange", "grape"], if you want to target the third element "orange", you would use the index 2 since counting starts from 0.

Can individual elements of an array be accessed? ›

The elements in an array are stored in contiguous memory locations. Each element can be accessed and modified using an index, which is a unique integer value.

How are individual array elements identified? ›

An array is a collection of like variables that share a single name. The individual elements of an array are referenced by appending a subscript, in square brackets, behind the name. The subscript itself can be any legitimate C expression that yields an integer value, even a general expression.

How do you find the amount of elements in an array? ›

The trick is to first find the size of the whole array in bytes and the size of a single element using the sizeof operator and then divide the size of the whole array by the size of a single element so that we can get the number of elements in the array.

How do you get all the elements in an array? ›

The every() method executes a function for each array element. The every() method returns true if the function returns true for all elements. The every() method returns false if the function returns false for one element. The every() method does not execute the function for empty elements.

How do you find the number of elements in an array list? ›

The length of an ArrayList in Java can be found using the size() method of the ArrayList class, with the syntax int size = arrayList. size(); . This method returns the number of elements present in the ArrayList.

How do you find a specific object in an array? ›

If we are looking for a specific object from an array of objects, we can use the find method. The find method returns the first element in the array that satisfies the provided testing function. If no elements pass the test, undefined is returned.

How do I access a specific value in an array? ›

Approach 1: Using square bracket notation

You can access elements in an array by using their index, where the index starts from 0 for the first element. We can access using the bracket notation. Example: This example shows the use of the above-explained approach.

How do you check if an array contains a certain element? ›

Description. The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.

How to find unique elements in array MATLAB? ›

Use unique to find the unique elements in the concatenated vector [x;y] . The unique function performs exact comparisons and determines that some values in x are not exactly equal to values in y .

References

Top Articles
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 5779

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.