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.
A = [10, 20, 30, 40, 50]; L = A > 25; B = A(L);
| Step | Variable | Value | Action | Output |
|---|---|---|---|---|
| 1 | A | [10 20 30 40 50] | Create array A | |
| 2 | L | [false false true true true] | Compare A > 25 to create logical mask | |
| 3 | B | [30 40 50] | Use L to select elements from A | [30 40 50] |
| 4 | - | - | End of execution |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| A | undefined | [10 20 30 40 50] | [10 20 30 40 50] | [10 20 30 40 50] | [10 20 30 40 50] |
| L | undefined | undefined | [false false true true true] | [false false true true true] | [false false true true true] |
| B | undefined | undefined | undefined | [30 40 50] | [30 40 50] |
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.