Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will select elements less than 5.
Using '==' will select only elements equal to 5.
✗ Incorrect
The operator '>' selects elements greater than 5 using logical indexing.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The operator '<' selects elements less than 3 to replace them with zero.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '~=' selects odd numbers instead of even.
Using '>' or '<' does not correctly identify even numbers.
✗ Incorrect
The '==' operator checks where the remainder is zero, selecting even numbers.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' excludes 3, which is incorrect.
Using '<' excludes 7, which is incorrect.
✗ Incorrect
Use '>=' for elements greater or equal to 3 and '<=' for elements less or equal to 7.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.*' multiplies by 2 instead of squaring.
Using '+' adds 2 instead of squaring.
✗ Incorrect
Select elements greater than 2 and map them to their squares using '.^ 2'.