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?
ASets elements of A greater than 10 to zero
BReturns a logical array of elements greater than 10
CSelects elements of A greater than 10 and stores them in B
DGenerates an error
✗ 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?
A[true false true]
B[1 2 3]
C[0 1 2]
D[5 6 7]
✗ 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?
AReturns all elements
BError due to size mismatch
CReturns first two elements
DReturns first element only
✗ 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?
AA(A < 0) = 0;
BA = 0 where A < 0;
CA = A > 0;
DA = zeros(size(A));
✗ Incorrect
Using logical indexing, you select elements less than zero and assign zero to them.
If idx = A > 3;, what type is idx?
AString array
BNumeric array
CCell array
DLogical array
✗ 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.
Practice
(1/5)
1. What does logical indexing in MATLAB allow you to do with an array A and a logical array L of the same size?
easy
A. Sort the elements of A based on L
B. Select elements of A where L is true
C. Change the size of A to match L
D. Convert A into a logical array
Solution
Step 1: Understand logical indexing concept
Logical indexing uses a logical array to pick elements from another array where the logical array is true.
Step 2: Apply to arrays A and L
Elements of A are selected only where L has true values, filtering A.
Final Answer:
Select elements of A where L is true -> Option B
Quick Check:
Logical indexing = select elements [OK]
Hint: Logical array true picks elements from original array [OK]