0
0
MATLABdata~10 mins

Logical indexing in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select elements greater than 5 from the array.

MATLAB
A = [2, 7, 4, 9, 1];
result = A(A [1] 5);
Drag options to blanks, or click blank then click option'
A>
B<
C==
D~=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will select elements less than 5.
Using '==' will select only elements equal to 5.
2fill in blank
medium

Complete the code to replace elements less than 3 with zero using logical indexing.

MATLAB
A = [1, 4, 2, 6, 3];
A(A [1] 3) = 0;
Drag options to blanks, or click blank then click option'
A<=
B>
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' will select elements greater than 3, which is incorrect here.
Using '<=' will include elements equal to 3, which is not intended.
3fill in blank
hard

Fix the error in the code to select even numbers using logical indexing.

MATLAB
A = [1, 2, 3, 4, 5];
evenNums = A(mod(A, 2) [1] 0);
Drag options to blanks, or click blank then click option'
A==
B<
C>
D~=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '~=' selects odd numbers instead of even.
Using '>' or '<' does not correctly identify even numbers.
4fill in blank
hard

Fill both blanks to create a logical index for elements between 3 and 7 (inclusive).

MATLAB
A = [1, 4, 6, 8, 3];
idx = (A [1] 3) & (A [2] 7);
Drag options to blanks, or click blank then click option'
A>=
B<
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' excludes 3, which is incorrect.
Using '<' excludes 7, which is incorrect.
5fill in blank
hard

Fill all three blanks to create a dictionary of elements and their squares for elements greater than 2.

MATLAB
A = [1, 3, 5, 2];
result = containers.Map(A(A [1] 2), A(A [2] 2) [3] 2);
Drag options to blanks, or click blank then click option'
A>
B.*
C.^
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.*' multiplies by 2 instead of squaring.
Using '+' adds 2 instead of squaring.