0% found this document useful (0 votes)
12 views5 pages

MATLAB Logical Indexing Explained

This document provides an overview of logical operations and indexing in MATLAB, including how to apply single and multiple conditions to arrays. It explains the use of logical operators and the find function to locate elements that meet specific criteria, as well as how to replace values in an array based on conditions. The document includes examples and code snippets to illustrate these concepts.

Uploaded by

takkkie556
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

MATLAB Logical Indexing Explained

This document provides an overview of logical operations and indexing in MATLAB, including how to apply single and multiple conditions to arrays. It explains the use of logical operators and the find function to locate elements that meet specific criteria, as well as how to replace values in an array based on conditions. The document includes examples and code snippets to illustrate these concepts.

Uploaded by

takkkie556
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MATLAB Lesson 3

Logic in MATLAB

The value 1

The value
Example
% a) -
0<x<10?

Answer
clc, clearvars
% to creat 100000 points between 0 and 10
x = linspace(0,10,100000);
y = sin(x);

% to plot x and y by using . as each point


plot(x,y,".")

% to plot a read line for y=0.8 and y by using . as each point


hold on, plot([0 10], [0.8 0.8], '-r')
y>=0.8

y_greater = y>=0.8;
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.

MATLAB 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.
Apply a Single Condition
I = find(A < 9)
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. I = 8×1
rng default 3
A = randi(15,5) 6
A = 5×5 7
11
13 2 3 3 10 14
14 5 15 7 1 16
2 9 15 14 13 17
14 15 8 12 15 22
10 15 13 15 11
The result is a column vector of linear indices. Each index describes the location of an element
Use the relational less than operator, <, to determine which elements of A are less than 9. Store the in A that is less than 9, so in practice A(I) returns the same result as A(B). The difference is
result in B. that A(B) uses logical indexing, whereas A(I) uses linear indexing.

B = A < 9 Apply Multiple Conditions


B = 5x5 logical array 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.
0 1 1 1 0 First, use the logical and operator, denoted &, to specify two conditions: the elements must be less
0 1 0 1 1 than 9 and greater than 2. Specify the conditions as a logical index to view the elements that satisfy
1 0 0 0 0 both conditions.
0 0 1 0 0
0 0 0 0 0 A(A<9 & A>2)
ans = 5×1
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 5
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 3
logical 1 (true). 8
3
Although B contains information about which elements in A are less than 9, it doesn’t tell you what 7
their values are. Rather than comparing the two matrices element by element, you can use B to index
into A.
The result is a list of the elements in A that satisfy both conditions. Be sure to specify each condition
A(B) with a separate statement connected by a logical operator. For example, you cannot specify the
ans = 8×1 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.
2
2 A(A<9 & ~mod(A,2))
5
ans = 3×1
3
8
2
3
2
7
8
1

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. that A&C returns a matrix of logical 0 (false) values and A|C returns a matrix of logical 1 (true)
values.
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

You might also like