0
0
MATLABdata~20 mins

Logical indexing in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logical Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A[10 20 30]
B[30 40 50]
C[25 30 40 50]
D[10 20 40 50]
Attempts:
2 left
💡 Hint
Logical indexing selects elements where the condition is true.
Predict Output
intermediate
2: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);
A[12 7 20]
B[12 20]
C[12 7]
D[5 12 7]
Attempts:
2 left
💡 Hint
Use & to combine conditions and select elements between 5 and 20, excluding 5 and 20.
Predict Output
advanced
2: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;
A[1 2 3; 4 0 0; 0 0 0]
B[1 2 3; 4 5 6; 7 8 0]
C[1 2 3; 0 0 0; 0 0 0]
D[0 0 0; 4 5 6; 7 8 9]
Attempts:
2 left
💡 Hint
Elements greater than 4 are replaced by zero.
Predict Output
advanced
2:00remaining
Logical indexing with empty result
What is the output of this code snippet?
MATLAB
A = [2, 4, 6, 8];
B = A(A < 0);
AError: Index exceeds matrix dimensions.
B[0]
C[2 4 6 8]
D[]
Attempts:
2 left
💡 Hint
No elements satisfy the condition A < 0.
🧠 Conceptual
expert
3:00remaining
Logical indexing behavior with NaN values
Given the vector V = [1, NaN, 3, NaN, 5], what is the result of V(~isnan(V))?
A[1 3 5]
B[NaN NaN]
C[1 NaN 3 NaN 5]
DError: Invalid logical indexing
Attempts:
2 left
💡 Hint
The ~ operator negates the logical array returned by isnan.