0
0
MATLABdata~5 mins

Logical indexing in MATLAB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is logical indexing in MATLAB?
Logical indexing is a way to select elements from an array using a logical array of the same size, where true means select the element and false means skip it.
Click to reveal answer
beginner
How do you create a logical index to select elements greater than 5 from array A?
Use the expression idx = A > 5; which returns a logical array where elements of A greater than 5 are true.
Click to reveal answer
beginner
Given A = [2, 7, 4, 9], what does A(A > 5) return?
It returns [7 9] because only elements greater than 5 are selected using logical indexing.
Click to reveal answer
intermediate
Can logical indexing be used to modify elements in an array? How?
Yes. You can assign new values to elements selected by logical indexing. For example, A(A < 3) = 0; sets all elements less than 3 in A to zero.
Click to reveal answer
intermediate
What happens if the logical index array is not the same size as the original array?
MATLAB will give an error because logical indexing requires the logical array to be the same size as the array being indexed.
Click to reveal answer
What does the expression B = A(A > 10); do in MATLAB?
ASets elements of A greater than 10 to zero
BReturns a logical array of elements greater than 10
CSelects elements of A greater than 10 and stores them in B
DGenerates an error
Which of these is a valid logical index for array A?
A[true false true]
B[1 2 3]
C[0 1 2]
D[5 6 7]
What will happen if you try A([true false]) when A has 3 elements?
AReturns all elements
BError due to size mismatch
CReturns first two elements
DReturns first element only
How can you change all negative values in array A to zero using logical indexing?
AA(A &lt; 0) = 0;
BA = 0 where A &lt; 0;
CA = A &gt; 0;
DA = zeros(size(A));
If idx = A > 3;, what type is idx?
AString array
BNumeric array
CCell array
DLogical array
Explain how logical indexing works in MATLAB and give an example of selecting elements from an array.
Think about how you can use true/false arrays to pick elements.
You got /5 concepts.
    Describe how to modify elements of an array using logical indexing in MATLAB.
    Consider how you can assign new values to parts of an array.
    You got /3 concepts.