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
not, respectively denoted by the symbols
Apply a Single Condition
Objects = findsystem(Name,Value) returns loaded systems and the objects in those systems that meet the criteria specified by one or more Name,Value pair arguments. You can use this syntax to specify search constraints and to search for specific parameter values. Specify the search constraints before the parameter and value pairs. Nonlinear programming solver. Iteration Func-count min f(x) Procedure 0 1 -6.70447 1 3 -6.89837 initial simplex 2 5 -7.34101 expand 3 7 -7.91894 expand 4 9 -9.07939 expand 5 11 -10.5047 expand 6 13 -12.4957 expand 7 15 -12.6957 reflect 8 17 -12.8052 contract outside 9 19 -12.8052 contract inside 10 21 -13.0189 expand 11 23 -13.0189 contract inside 12 25 -13.0374 reflect 13 27 -13.122 reflect. MATLAB ® always treats the. character as a wildcard, even on file systems that support. in file names. The MATLAB dir function is consistent with the Microsoft ® Windows ® operating system dir command in that both support short file names generated by DOS. If A is a vector, then max(A) returns the maximum of A. If A is a matrix, then max(A) is a row vector containing the maximum value of each column. If A is a multidimensional array, then max(A) operates along the first array dimension whose size does not equal 1, treating the elements as vectors.
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.
Use the relational less than operator,
<, to determine which elements of
A are less than 9. Store the result in
The result is a logical matrix. Each value in
B represents a logical
true) or logical
false) state to indicate whether the corresponding element of
A fulfills the condition
A < 9. For example,
B(1,1) is logical
B(1,2) is logical
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
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.
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
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.
The result is a list of the elements in
A that satisfy both conditions. Uranium atomic mass rounded. 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
Next, find the elements in
A that are less than 9 and even numbered.
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
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.
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.
find function to get the index of the element equal to 8 that satisfies the conditions.
Matlab Find Command
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.
Matlab Find Value In Matrix
Replace all values in
A that are greater than 10 with the number 10.
Next, replace all values in
A that are not equal to 10 with a
Lastly, replace all of the
NaN values in
A with zeros and apply the logical NOT operator,
The resulting matrix has values of logical
true) in place of the
NaN values, and logical
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
false) values and
A C returns a matrix of logical
Logical Operators: Short Circuit