Bird
Raised Fist0
MATLABdata~10 mins

Logical indexing in MATLAB - Step-by-Step Execution

Choose your learning style10 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.

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

  1. Step 1: Understand logical indexing concept

    Logical indexing uses a logical array to pick elements from another array where the logical array is true.
  2. Step 2: Apply to arrays A and L

    Elements of A are selected only where L has true values, filtering A.
  3. Final Answer:

    Select elements of A where L is true -> Option B
  4. Quick Check:

    Logical indexing = select elements [OK]
Hint: Logical array true picks elements from original array [OK]
Common Mistakes:
  • Thinking logical indexing changes array size arbitrarily
  • Confusing logical indexing with sorting
  • Assuming logical indexing converts data types
2. Which of the following is the correct syntax to select elements of vector v that are greater than 5 using logical indexing?
easy
A. v(v > 5)
B. v > 5(v)
C. v{v > 5}
D. v[v > 5]

Solution

  1. Step 1: Identify correct logical indexing syntax

    In MATLAB, logical indexing uses parentheses with a logical condition inside, like v(condition).
  2. Step 2: Check each option

    v(v > 5) uses v(v > 5), which is correct syntax. The other options use invalid indexing forms.
  3. Final Answer:

    v(v > 5) -> Option A
  4. Quick Check:

    Logical indexing syntax = v(condition) [OK]
Hint: Use parentheses with condition inside for logical indexing [OK]
Common Mistakes:
  • Using curly braces or square brackets for logical indexing
  • Placing condition outside parentheses
  • Confusing logical indexing with cell or array indexing
3. Given A = [3, 7, 2, 9, 5], what is the result of A(A > 4)?
medium
A. [2, 5]
B. [3, 7, 2, 9, 5]
C. [7, 9]
D. [7, 9, 5]

Solution

  1. Step 1: Create logical array for condition A > 4

    Check each element: 3>4?false, 7>4?true, 2>4?false, 9>4?true, 5>4?true. Logical array = [false, true, false, true, true].
  2. Step 2: Select elements of A where logical array is true

    Elements at true positions: 7, 9, 5.
  3. Final Answer:

    [7, 9, 5] -> Option D
  4. Quick Check:

    Elements > 4 = [7, 9, 5] [OK]
Hint: Filter elements by condition, then pick true positions [OK]
Common Mistakes:
  • Including elements not greater than 4
  • Confusing logical indexing with logical AND operation
  • Returning original array instead of filtered
4. What is wrong with this MATLAB code snippet?
A = [1, 4, 6, 8];
L = [true, false];
B = A(L);
medium
A. Logical array L size does not match A size
B. Logical array L must contain only numeric values
C. Cannot use logical indexing on row vectors
D. Syntax error in logical indexing

Solution

  1. Step 1: Check sizes of A and L

    A has 4 elements, L has 2 elements. Logical indexing requires L to be same size as A.
  2. Step 2: Identify error cause

    Since sizes differ, MATLAB will give an error when using L to index A.
  3. Final Answer:

    Logical array L size does not match A size -> Option A
  4. Quick Check:

    Logical array size must match data size [OK]
Hint: Logical array must be same size as indexed array [OK]
Common Mistakes:
  • Assuming logical array can be smaller
  • Thinking logical array must be numeric
  • Believing logical indexing syntax is wrong here
5. You have a matrix M = [2, 5, 8; 1, 7, 3; 4, 6, 9]. How do you replace all elements less than 5 with zero using logical indexing?
hard
A. M(M <= 5) = 0;
B. M(M > 5) = 0;
C. M(M < 5) = 0;
D. M(M == 5) = 0;

Solution

  1. Step 1: Identify elements less than 5 in M

    Elements less than 5 are 2, 1, 3, 4.
  2. Step 2: Use logical indexing to assign zero to these elements

    Using M(M < 5) = 0; replaces all elements less than 5 with zero.
  3. Final Answer:

    M(M < 5) = 0; -> Option C
  4. Quick Check:

    Replace elements < 5 with 0 using M(M < 5) = 0 [OK]
Hint: Use condition inside parentheses to assign new values [OK]
Common Mistakes:
  • Replacing wrong elements by using > or == instead of <
  • Using incorrect logical operators
  • Trying to assign without logical indexing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard