Recall & Review
beginner
What is logical indexing in MATLAB?
Logical indexing is a way to select elements from an array using a logical array of the same size, where
true means select the element and false means skip it.Click to reveal answer
beginner
How do you create a logical index to select elements greater than 5 from array
A?Use the expression
idx = A > 5; which returns a logical array where elements of A greater than 5 are true.Click to reveal answer
beginner
Given
A = [2, 7, 4, 9], what does A(A > 5) return?It returns
[7 9] because only elements greater than 5 are selected using logical indexing.Click to reveal answer
intermediate
Can logical indexing be used to modify elements in an array? How?
Yes. You can assign new values to elements selected by logical indexing. For example,
A(A < 3) = 0; sets all elements less than 3 in A to zero.Click to reveal answer
intermediate
What happens if the logical index array is not the same size as the original array?
MATLAB will give an error because logical indexing requires the logical array to be the same size as the array being indexed.
Click to reveal answer
What does the expression
B = A(A > 10); do in MATLAB?✗ Incorrect
The expression uses logical indexing to select elements of A that are greater than 10 and stores them in B.
Which of these is a valid logical index for array
A?✗ Incorrect
Logical indices must be logical arrays (true/false). Numeric arrays like [1 2 3] are not valid logical indices.
What will happen if you try
A([true false]) when A has 3 elements?✗ Incorrect
Logical index arrays must be the same size as the array being indexed, otherwise MATLAB throws an error.
How can you change all negative values in array
A to zero using logical indexing?✗ Incorrect
Using logical indexing, you select elements less than zero and assign zero to them.
If
idx = A > 3;, what type is idx?✗ Incorrect
The result of a comparison like A > 3 is a logical array of true/false values.
Explain how logical indexing works in MATLAB and give an example of selecting elements from an array.
Think about how you can use true/false arrays to pick elements.
You got /5 concepts.
Describe how to modify elements of an array using logical indexing in MATLAB.
Consider how you can assign new values to parts of an array.
You got /3 concepts.