Matlab Vectorization & Performance Optimization Quiz
40 high-impact questions on speeding up MATLAB code using vectorization, element-wise operations, preallocation, and profiling tools like tic/toc.
Question 1
What is the primary definition of 'vectorization' in the context of MATLAB programming?
Question 2
Which of the following operations represents an element-wise multiplication of two matrices A and B?
Question 3
Why is preallocating arrays (e.g., using zeros or ones) recommended before a loop?
Question 4
You need to calculate the sine of 1,000,000 numbers stored in vector 'x'. Which approach is most efficient?
x = rand(1, 1e6);Question 5
What does the 'tic' and 'toc' command pair do?
Question 6
Which function allows you to apply a function to each cell in a cell array without writing a loop?
Question 7
Consider the operation 'A .^ 2'. What does this compute?
Question 8
What is 'Implicit Expansion' (introduced in R2016b)?
Question 9
Which code snippet correctly preallocates a 1000x1000 matrix of doubles?
% Goal: Create a 1000x1000 matrix initialized to zeroQuestion 10
Why is 'arrayfun' sometimes slower than a simple for-loop?
Question 11
What is the result of 'sum(A)' if A is a 2D matrix?
Question 12
How does MATLAB store 2D matrices in memory?
Question 13
Which command would you use to measure the execution time of a function more accurately than tic/toc, by running it multiple times?
Question 14
You want to replace all values greater than 10 in matrix M with 10. Which vectorized line does this?
M = [12, 5; 8, 15];Question 15
What is the purpose of the 'profile viewer' command?
Question 16
Which operation is generally faster for calculating the Euclidean norm of vectors stored in columns?
Question 17
When using 'repmat' to replicate a matrix, what is a potential downside compared to implicit expansion?
Question 18
How can you vectorize a conditional calculation like 'if x > 0, y = x; else y = 0; end' for an array x?
Question 19
What does 'bsxfun' stand for?
Question 20
Which approach is best for creating a sparse matrix if you know the indices and values beforehand?
Question 21
What is the result of 'A(:)'?
Question 22
Which function computes the matrix product of two arrays?
Question 23
In a loop 'for k=1:N, A(k) = k^2; end', what is the main performance issue if A is not defined beforehand?
Question 24
How would you efficiently compute the moving average of a vector 'x' with a window size of 3?
Question 25
What does 'JIT' stand for in the context of MATLAB performance?
Question 26
Which code creates a 1D grid of 100 points from 0 to 1?
Question 27
You want to compute the difference between adjacent elements in a vector. Which function does this?
Question 28
What is the danger of using 'inv(A) * b' to solve linear systems?
Question 29
Which function efficiently applies a logical AND across all columns of a logical matrix?
Question 30
How do you perform a matrix-vector multiplication where the vector is treated as a diagonal matrix, without creating the diagonal matrix?
A = rand(3,3); v = [1; 2; 3];
% Goal: Multiply each column j of A by v(j)Question 31
What does 'meshgrid' do, and why is it useful for vectorization?
Question 32
Which data type uses the least memory for storing simple integers from 0 to 255 (e.g., image data)?
Question 33
What is the result of 'cumsum([1, 2, 3, 4])'?
Question 34
Which function is used to find unique rows in a matrix?
Question 35
What is the effect of 'A = A.' (transpose) on a complex matrix?
Question 36
How can you efficiently check if two arrays A and B are exactly equal?
Question 37
What is the fastest way to compute the sum of squares of a vector 'x'?
Question 38
Which command clears all variables from memory to free up RAM?
Question 39
When using 'gpuArray', what happens to the data?
Question 40
What is the result of 'reshape(1:6, 2, 3)'?
