Matlab Logical Indexing & Masking Quiz

Matlab
0 Passed
0% acceptance

Master MATLAB's powerful logical indexing techniques with 35 questions covering boolean masks, conditional filtering, find(), and array manipulation without loops.

35 Questions
~70 minutes
1

Question 1

What is the primary requirement for a logical array used as a mask to index another array 'A'?

A
It must have the exact same dimensions (size) as 'A'
B
It must be a row vector
C
It must contain only 0s and 1s of type double
D
It must be smaller than 'A'
2

Question 2

What is the result of the following operation?

matlab
A = [10, 20, 30, 40];
mask = [true, false, true, false];
result = A(mask);
A
[10, 30]
B
[10, 0, 30, 0]
C
[20, 40]
D
[1, 3]
3

Question 3

When you use a logical mask to index into a 2D matrix (e.g., A(mask)), what is the shape of the returned result?

A
A column vector containing all selected elements
B
A matrix of the same size as A with zeros where the mask is false
C
A row vector
D
It preserves the original matrix shape
4

Question 4

You want to clamp all negative values in vector A to 0. Which command achieves this?

matlab
A = [1, -5, 3, -2];
% Goal: [1, 0, 3, 0]
A
A(A < 0) = 0;
B
A(A < 0) == 0;
C
find(A < 0) = 0;
D
if A < 0, A = 0; end
5

Question 5

Which operator represents the element-wise logical 'OR' used for combining array masks?

A
|
B
||
C
OR
D
+
6

Question 6

What does the following code return?

matlab
A = [5, 10, 15];
idx = find(A > 20);
isempty(idx)
A
logical 1 (true)
B
logical 0 (false)
C
[]
D
Error
7

Question 7

What is a key advantage of using logical indexing 'A(mask)' over 'A(find(mask))'?

A
It is generally faster and uses less memory because it avoids creating an intermediate index array
B
It allows you to access elements out of order
C
It works on cell arrays while find does not
D
There is no difference
8

Question 8

What is the content of 'result' after executing this code?

matlab
A = [1, 2, 3, 4, 5];
result = A(A > 2 & A < 5);
A
[3, 4]
B
[3, 4, 5]
C
[2, 3, 4]
D
[1, 2, 5]
9

Question 9

Which operator is used to invert a logical mask (Logical NOT)?

A
~
B
!
C
not
D
-
10

Question 10

How can you remove all NaN (Not-a-Number) values from a vector 'data'?

matlab
data = [NaN, 1, 2, NaN, 3];
A
data(isnan(data)) = [];
B
data(data == NaN) = [];
C
remove(data, NaN);
D
data = data - NaN;
11

Question 11

If 'A' is a 3x3 matrix and 'mask' is a 3x3 logical matrix, what does the command 'A(mask) = 100;' do?

A
It assigns the value 100 to every element in A where the corresponding element in mask is true
B
It replaces the entire matrix A with 100
C
It creates a new vector of 100s
D
It errors because you cannot assign a scalar to multiple elements
12

Question 12

What is the result of the following code?

matlab
A = [10, 20];
mask = logical([1, 0]);
B = A(mask);
A
10
B
[10, 20]
C
20
D
[10, 0]
13

Question 13

What happens if you attempt 'A(mask)' where 'mask' is a logical vector shorter than 'A'?

A
MATLAB throws an error (Index exceeds array bounds or dimension mismatch)
B
It assumes false for the missing elements
C
It recycles the mask to fit the size of A
D
It assumes true for the missing elements
14

Question 14

You have a matrix 'data' where rows represent samples. How do you select all rows where the first column is greater than 5?

matlab
data = magic(4);
A
data(data(:, 1) > 5, :)
B
data(data > 5, :)
C
data(1 > 5, :)
D
data(find(data > 5))
15

Question 15

Which function returns true if *at least one* element in a logical array is true?

A
any()
B
all()
C
some()
D
exists()
16

Question 16

What is the result of this code, which flips the sign of even numbers?

matlab
vals = [1, 2, 3, 4];
mask = rem(vals, 2) == 0;
vals(mask) = -vals(mask);
A
[1, -2, 3, -4]
B
[-1, 2, -3, 4]
C
[1, 2, 3, 4]
D
Error
17

Question 17

Which syntax correctly returns both row and column indices of non-zero elements in matrix X?

A
[r, c] = find(X)
B
find(X, 'rows', 'cols')
C
indices(X)
D
[r, c] = where(X)
18

Question 18

What is the result of indexing an array with a numeric vector of 0s and 1s (of type double) instead of logicals?

matlab
A = [10, 20, 30];
idx = [1, 0, 1]; % double, not logical
C = A(idx);
A
Error (Index 0 is invalid)
B
[10, 30]
C
[10, 10, 10]
D
[10, 20, 30]
19

Question 19

What happens if you try to convert a vector containing NaN values to logical using 'logical(vec)'?

A
Error: NaNs cannot be converted to logicals
B
NaNs become true
C
NaNs become false
D
NaNs remain NaN
20

Question 20

How many vowels are in the string?

matlab
str = 'Matlab';
vowels = str == 'a';
num = sum(vowels);
A
2
B
1
C
6
D
0
21

Question 21

Can you use logical indexing on the left side of an assignment to change the shape of an array (e.g., delete elements)?

A
Yes, by assigning [] (empty) to the selected elements
B
No, you can never change shape
C
Yes, by assigning 0
D
Yes, by assigning NaN
22

Question 22

What is the content of A after this operation?

matlab
A = 1:5;
A(A > 3) = A(A > 3) * 2;
A
[1, 2, 3, 8, 10]
B
[1, 2, 3, 4, 5]
C
[2, 4, 6, 8, 10]
D
[1, 2, 3, 4, 10]
23

Question 23

Which operator signifies 'Not Equal' in MATLAB?

A
~=
B
!=
C
<>
D
!==
24

Question 24

What is the result of logically negating a numeric vector containing zeros and non-zeros?

matlab
A = [0, 5, 0];
B = ~A;
A
[1, 0, 1] (logical)
B
[1, -4, 1]
C
[0, 1, 0]
D
Error
25

Question 25

What does the 'xor(A, B)' function return?

A
True where either A or B is true, but NOT both (Exclusive OR)
B
True where both A and B are true
C
True where either A or B is true (Inclusive OR)
D
True where A and B are both false
26

Question 26

What is the final value of A?

matlab
A = [10, 20, 30];
mask = A > 15;
A(~mask) = 0;
A
[0, 20, 30]
B
[10, 0, 0]
C
[10, 20, 30]
D
[0, 0, 0]
27

Question 27

Why might 'A(A == 0.3)' fail to return expected values if A contains computed floating-point numbers?

A
Floating-point precision errors may make values slightly different from 0.3
B
0.3 is not a valid number in MATLAB
C
Logical indexing doesn't work with decimals
D
It requires single quotes
28

Question 28

What does 'find' return for a matrix when only one output is requested?

matlab
A = [1, 2; 3, 4];
idx = find(A > 2);
A
Linear indices [2; 4] (corresponding to values 3 and 4)
B
Values [3; 4]
C
Row indices [2; 2]
D
A logical mask
29

Question 29

Which statement best describes the efficiency of logical indexing vs loops in MATLAB?

A
Logical indexing is vectorized and typically much faster than explicit for-loops
B
Loops are always faster in MATLAB
C
They have identical performance
D
Logical indexing uses more memory so it is slower
30

Question 30

What happens here?

matlab
A = [1, 2, 3];
mask = [true, true];
A(mask)
A
Error: dimension mismatch
B
[1, 2]
C
[1, 2, 0]
D
[1, 2, 3]
31

Question 31

Is it necessary to use 'find' to simply count how many elements satisfy a condition?

A
No, sum(condition) is sufficient and often faster
B
Yes, find is the only way to count
C
No, use length(condition)
D
Yes, because sum() only adds numbers
32

Question 32

What is the value of 'count'?

matlab
A = [1, 2, 3];
count = sum(A > 1);
A
2
B
1
C
3
D
5
33

Question 33

What is the primary benefit of 'vectorization' using logical indexing?

A
It avoids explicit loops, leading to cleaner, more readable, and often faster code
B
It allows code to run on GPUs only
C
It uses less memory than scalar operations
D
It automatically parallelizes code across clusters
34

Question 34

Does modifying the extracted subset 'C' affect the original array 'A'?

matlab
A = [10, 20, 30];
B = [true, false, true];
C = A(B);
C(1) = 99;
A
No, C is a separate copy of the values
B
Yes, A becomes [99, 20, 30]
C
Yes, A becomes [99, 20, 99]
D
It depends on the MATLAB version
35

Question 35

Can logical indexing be used directly on cell arrays?

A
Yes, it extracts a subset of cells as a new cell array
B
No, it only works on numeric arrays
C
Yes, but it converts contents to doubles
D
No, it errors immediately

QUIZZES IN Matlab