Challenge - 5 Problems
Logical Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of logical indexing with condition
What is the output of the following MATLAB code?
MATLAB
A = [10, 20, 30, 40, 50]; B = A(A > 25);
Attempts:
2 left
💡 Hint
Logical indexing selects elements where the condition is true.
✗ Incorrect
The condition A > 25 is true for elements 30, 40, and 50. So B contains these values.
❓ Predict Output
intermediate2:00remaining
Logical indexing with multiple conditions
What does the variable C contain after running this code?
MATLAB
A = [5, 12, 7, 20, 3]; C = A(A > 5 & A < 20);
Attempts:
2 left
💡 Hint
Use & to combine conditions and select elements between 5 and 20, excluding 5 and 20.
✗ Incorrect
Only 12 and 7 satisfy both conditions: greater than 5 and less than 20.
❓ Predict Output
advanced2:00remaining
Logical indexing with matrix and assignment
What is the value of matrix M after executing this code?
MATLAB
M = [1 2 3; 4 5 6; 7 8 9]; M(M > 4) = 0;
Attempts:
2 left
💡 Hint
Elements greater than 4 are replaced by zero.
✗ Incorrect
All elements greater than 4 are set to zero, so 5,6,7,8,9 become 0.
❓ Predict Output
advanced2:00remaining
Logical indexing with empty result
What is the output of this code snippet?
MATLAB
A = [2, 4, 6, 8]; B = A(A < 0);
Attempts:
2 left
💡 Hint
No elements satisfy the condition A < 0.
✗ Incorrect
Since no elements are less than zero, the result is an empty array.
🧠 Conceptual
expert3:00remaining
Logical indexing behavior with NaN values
Given the vector V = [1, NaN, 3, NaN, 5], what is the result of V(~isnan(V))?
Attempts:
2 left
💡 Hint
The ~ operator negates the logical array returned by isnan.
✗ Incorrect
isnan(V) returns true for NaN elements; ~isnan(V) is true for non-NaN elements, so only 1,3,5 are selected.