0
0
MATLABdata~10 mins

Logical indexing in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logical indexing
Create array A
Create logical mask L
Use L to select elements from A
Output selected elements
End
Logical indexing uses a true/false mask to pick elements from an array.
Execution Sample
MATLAB
A = [10, 20, 30, 40, 50];
L = A > 25;
B = A(L);
Selects elements from A that are greater than 25 using logical indexing.
Execution Table
StepVariableValueActionOutput
1A[10 20 30 40 50]Create array A
2L[false false true true true]Compare A > 25 to create logical mask
3B[30 40 50]Use L to select elements from A[30 40 50]
4--End of execution
💡 All steps completed; logical indexing selected elements > 25.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Aundefined[10 20 30 40 50][10 20 30 40 50][10 20 30 40 50][10 20 30 40 50]
Lundefinedundefined[false false true true true][false false true true true][false false true true true]
Bundefinedundefinedundefined[30 40 50][30 40 50]
Key Moments - 3 Insights
Why does the logical mask L have true only for some elements?
Because L is created by comparing each element of A to 25, only elements greater than 25 get true (see step 2 in execution_table).
What happens if we use L to index A?
Only elements where L is true are selected from A, producing B with those elements (see step 3 in execution_table).
Can logical indexing change the size of the output array?
Yes, the output array B only contains elements where L is true, so it can be smaller than A (see step 3 output).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of L?
A[true true false false false]
B[false false true true true]
C[true false true false true]
D[false true false true false]
💡 Hint
Check the 'Value' column for L at step 2 in the execution_table.
At which step does B get its value assigned?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for when B is created and assigned in the execution_table.
If A was [5 15 25 35 45], what would be the value of L after step 2?
A[false true true true true]
B[true true true false false]
C[false false false true true]
D[true false false false true]
💡 Hint
Compare each element of A to 25 to create L as in step 2.
Concept Snapshot
Logical indexing in MATLAB:
- Create a logical mask by comparing array elements.
- Use mask to select elements: B = A(L).
- Output contains only elements where mask is true.
- Useful for filtering arrays easily.
Full Transcript
Logical indexing in MATLAB lets you pick elements from an array by using a true/false mask. First, you create an array A. Then you make a logical mask L by comparing A to a condition, like A > 25. This mask has true where the condition is met and false elsewhere. Using A(L) selects only the elements where L is true. For example, if A = [10 20 30 40 50], then L = A > 25 gives [false false true true true]. Using B = A(L) results in B = [30 40 50]. This method is simple and powerful for filtering data.