Matlab Logical Indexing & Masking Quiz
Master MATLAB's powerful logical indexing techniques with 35 questions covering boolean masks, conditional filtering, find(), and array manipulation without loops.
Question 1
What is the primary requirement for a logical array used as a mask to index another array 'A'?
Question 2
What is the result of the following operation?
A = [10, 20, 30, 40];
mask = [true, false, true, false];
result = A(mask);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?
Question 4
You want to clamp all negative values in vector A to 0. Which command achieves this?
A = [1, -5, 3, -2];
% Goal: [1, 0, 3, 0]Question 5
Which operator represents the element-wise logical 'OR' used for combining array masks?
Question 6
What does the following code return?
A = [5, 10, 15];
idx = find(A > 20);
isempty(idx)Question 7
What is a key advantage of using logical indexing 'A(mask)' over 'A(find(mask))'?
Question 8
What is the content of 'result' after executing this code?
A = [1, 2, 3, 4, 5];
result = A(A > 2 & A < 5);Question 9
Which operator is used to invert a logical mask (Logical NOT)?
Question 10
How can you remove all NaN (Not-a-Number) values from a vector 'data'?
data = [NaN, 1, 2, NaN, 3];Question 11
If 'A' is a 3x3 matrix and 'mask' is a 3x3 logical matrix, what does the command 'A(mask) = 100;' do?
Question 12
What is the result of the following code?
A = [10, 20];
mask = logical([1, 0]);
B = A(mask);Question 13
What happens if you attempt 'A(mask)' where 'mask' is a logical vector shorter than 'A'?
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?
data = magic(4);Question 15
Which function returns true if *at least one* element in a logical array is true?
Question 16
What is the result of this code, which flips the sign of even numbers?
vals = [1, 2, 3, 4];
mask = rem(vals, 2) == 0;
vals(mask) = -vals(mask);Question 17
Which syntax correctly returns both row and column indices of non-zero elements in matrix X?
Question 18
What is the result of indexing an array with a numeric vector of 0s and 1s (of type double) instead of logicals?
A = [10, 20, 30];
idx = [1, 0, 1]; % double, not logical
C = A(idx);Question 19
What happens if you try to convert a vector containing NaN values to logical using 'logical(vec)'?
Question 20
How many vowels are in the string?
str = 'Matlab';
vowels = str == 'a';
num = sum(vowels);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)?
Question 22
What is the content of A after this operation?
A = 1:5;
A(A > 3) = A(A > 3) * 2;Question 23
Which operator signifies 'Not Equal' in MATLAB?
Question 24
What is the result of logically negating a numeric vector containing zeros and non-zeros?
A = [0, 5, 0];
B = ~A;Question 25
What does the 'xor(A, B)' function return?
Question 26
What is the final value of A?
A = [10, 20, 30];
mask = A > 15;
A(~mask) = 0;Question 27
Why might 'A(A == 0.3)' fail to return expected values if A contains computed floating-point numbers?
Question 28
What does 'find' return for a matrix when only one output is requested?
A = [1, 2; 3, 4];
idx = find(A > 2);Question 29
Which statement best describes the efficiency of logical indexing vs loops in MATLAB?
Question 30
What happens here?
A = [1, 2, 3];
mask = [true, true];
A(mask)Question 31
Is it necessary to use 'find' to simply count how many elements satisfy a condition?
Question 32
What is the value of 'count'?
A = [1, 2, 3];
count = sum(A > 1);Question 33
What is the primary benefit of 'vectorization' using logical indexing?
Question 34
Does modifying the extracted subset 'C' affect the original array 'A'?
A = [10, 20, 30];
B = [true, false, true];
C = A(B);
C(1) = 99;Question 35
Can logical indexing be used directly on cell arrays?
