Matlab Vectorization & Performance Optimization Quiz

Matlab
0 Passed
0% acceptance

40 high-impact questions on speeding up MATLAB code using vectorization, element-wise operations, preallocation, and profiling tools like tic/toc.

40 Questions
~80 minutes
1

Question 1

What is the primary definition of 'vectorization' in the context of MATLAB programming?

A
Replacing explicit loops with array operations that process multiple elements simultaneously
B
Converting all variables into column vectors
C
Using the 'vector' class for all data storage
D
Compiling MATLAB code into C++ vectors
2

Question 2

Which of the following operations represents an element-wise multiplication of two matrices A and B?

A
A .* B
B
A * B
C
dot(A, B)
D
multiply(A, B)
3

Question 3

Why is preallocating arrays (e.g., using zeros or ones) recommended before a loop?

A
It prevents MATLAB from repeatedly resizing the array and copying memory in every iteration
B
It ensures the array contains valid data types
C
It is required syntax; loops cannot write to empty arrays
D
It automatically parallelizes the loop
4

Question 4

You need to calculate the sine of 1,000,000 numbers stored in vector 'x'. Which approach is most efficient?

matlab
x = rand(1, 1e6);
A
y = sin(x);
B
for i=1:length(x), y(i) = sin(x(i)); end
C
y = arrayfun(@sin, x);
D
y = map(sin, x);
5

Question 5

What does the 'tic' and 'toc' command pair do?

A
Measures the elapsed time of the code executed between them
B
Starts and stops the MATLAB debugger
C
Checks for syntax errors in the block
D
Pauses execution for a specified duration
6

Question 6

Which function allows you to apply a function to each cell in a cell array without writing a loop?

A
cellfun
B
arrayfun
C
structfun
D
apply
7

Question 7

Consider the operation 'A .^ 2'. What does this compute?

A
The square of each individual element in A
B
The matrix A multiplied by itself (A * A)
C
The square root of A
D
The power of 2 raised to A
8

Question 8

What is 'Implicit Expansion' (introduced in R2016b)?

A
Automatically expanding arrays of compatible sizes (e.g., adding a column vector to a matrix) without bsxfun
B
Expanding the memory limit of MATLAB automatically
C
Converting sparse matrices to full matrices implicitly
D
A method to hide variables from the workspace
9

Question 9

Which code snippet correctly preallocates a 1000x1000 matrix of doubles?

matlab
% Goal: Create a 1000x1000 matrix initialized to zero
A
A = zeros(1000, 1000);
B
A = []; A(1000, 1000) = 0;
C
A = matrix(1000, 1000);
D
for i=1:1000, for j=1:1000, A(i,j)=0; end; end
10

Question 10

Why is 'arrayfun' sometimes slower than a simple for-loop?

A
It still incurs function-call overhead for each element and doesn't always use compiled libraries
B
It runs on the GPU which has high latency
C
It converts data to text before processing
D
It is a deprecated function
11

Question 11

What is the result of 'sum(A)' if A is a 2D matrix?

A
A row vector containing the sum of each column
B
A column vector containing the sum of each row
C
The sum of all elements in the matrix
D
The cumulative sum of the matrix
12

Question 12

How does MATLAB store 2D matrices in memory?

A
Column-major order (columns are contiguous)
B
Row-major order (rows are contiguous)
C
As a linked list of numbers
D
Randomly hashed
13

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?

A
timeit
B
bench
C
profile
D
clock
14

Question 14

You want to replace all values greater than 10 in matrix M with 10. Which vectorized line does this?

matlab
M = [12, 5; 8, 15];
A
M(M > 10) = 10;
B
M = min(M, 10);
C
Both A and B work
D
if M > 10, M = 10; end
15

Question 15

What is the purpose of the 'profile viewer' command?

A
To open a graphical interface showing which lines of code consume the most time
B
To view the variables currently in the workspace
C
To plot the output of a function
D
To check for syntax errors
16

Question 16

Which operation is generally faster for calculating the Euclidean norm of vectors stored in columns?

A
vecnorm(A)
B
sqrt(sum(A.^2))
C
arrayfun(@norm, A)
D
looping through columns
17

Question 17

When using 'repmat' to replicate a matrix, what is a potential downside compared to implicit expansion?

A
It creates a larger array in memory, consuming more RAM
B
It is slower to type
C
It only works on integers
D
It cannot handle 3D arrays
18

Question 18

How can you vectorize a conditional calculation like 'if x > 0, y = x; else y = 0; end' for an array x?

A
y = x .* (x > 0);
B
y = max(x, 0);
C
Both A and B work
D
y = if(x > 0, x, 0);
19

Question 19

What does 'bsxfun' stand for?

A
Binary Singleton Expansion Function
B
Basic System Extension Function
C
Best Speed Execution Function
D
Block Solver X Function
20

Question 20

Which approach is best for creating a sparse matrix if you know the indices and values beforehand?

A
S = sparse(i, j, v, m, n);
B
Initialize S = zeros(m,n) and fill loop
C
Initialize S = spalloc(m,n,nz) and fill loop
D
S = eye(m,n)
21

Question 21

What is the result of 'A(:)'?

A
It reshapes A into a single column vector
B
It creates a copy of A
C
It selects the diagonal elements
D
It returns the last element
22

Question 22

Which function computes the matrix product of two arrays?

A
mtimes or *
B
times or .*
C
prod
D
dot
23

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?

A
The array A grows in size at every iteration
B
The calculation k^2 is slow
C
The loop counter k takes too much memory
D
There is no issue
24

Question 24

How would you efficiently compute the moving average of a vector 'x' with a window size of 3?

A
movmean(x, 3)
B
Loop through x and average neighbors
C
conv(x, [1/3, 1/3, 1/3], 'same')
D
Both A and C are vectorized and efficient
25

Question 25

What does 'JIT' stand for in the context of MATLAB performance?

A
Just-In-Time Compilation
B
Java Interface Tool
C
Joint Integration Test
D
Jump Instruction Table
26

Question 26

Which code creates a 1D grid of 100 points from 0 to 1?

A
linspace(0, 1, 100)
B
0:1:100
C
0:0.01:1
D
vector(0, 1, 100)
27

Question 27

You want to compute the difference between adjacent elements in a vector. Which function does this?

A
diff
B
gradient
C
sub
D
minus
28

Question 28

What is the danger of using 'inv(A) * b' to solve linear systems?

A
It is slower and less numerically stable than 'A \ b'
B
It is deprecated
C
It only works for 2x2 matrices
D
It returns integers only
29

Question 29

Which function efficiently applies a logical AND across all columns of a logical matrix?

A
all(A, 1)
B
any(A, 1)
C
and(A)
D
sum(A) == size(A,1)
30

Question 30

How do you perform a matrix-vector multiplication where the vector is treated as a diagonal matrix, without creating the diagonal matrix?

matlab
A = rand(3,3); v = [1; 2; 3];
% Goal: Multiply each column j of A by v(j)
A
A .* v'
B
A * diag(v)
C
A * v
D
bsxfun(@times, A, v)
31

Question 31

What does 'meshgrid' do, and why is it useful for vectorization?

A
It creates 2D grid coordinate matrices from 1D vectors, allowing vectorized evaluation of functions of two variables
B
It plots a 3D mesh
C
It measures the size of a grid
D
It interpolates data
32

Question 32

Which data type uses the least memory for storing simple integers from 0 to 255 (e.g., image data)?

A
uint8
B
double
C
int64
D
single
33

Question 33

What is the result of 'cumsum([1, 2, 3, 4])'?

A
[1, 3, 6, 10]
B
[1, 2, 3, 4]
C
10
D
[4, 3, 2, 1]
34

Question 34

Which function is used to find unique rows in a matrix?

A
unique(A, 'rows')
B
distinct(A)
C
find(A)
D
union(A)
35

Question 35

What is the effect of 'A = A.' (transpose) on a complex matrix?

A
It performs a non-conjugate transpose (swaps rows/cols but keeps values)
B
It performs a conjugate transpose (Hermitian)
C
It does nothing
D
It errors
36

Question 36

How can you efficiently check if two arrays A and B are exactly equal?

A
isequal(A, B)
B
A == B
C
all(A == B)
D
sum(A - B) == 0
37

Question 37

What is the fastest way to compute the sum of squares of a vector 'x'?

A
x * x' (if x is row vector)
B
sum(x.^2)
C
norm(x)^2
D
All are comparable, but A uses BLAS
38

Question 38

Which command clears all variables from memory to free up RAM?

A
clear
B
clc
C
close all
D
delete
39

Question 39

When using 'gpuArray', what happens to the data?

A
It is transferred from CPU RAM to GPU memory
B
It is compressed
C
It is converted to integers
D
It is written to disk
40

Question 40

What is the result of 'reshape(1:6, 2, 3)'?

A
[1, 3, 5; 2, 4, 6]
B
[1, 2, 3; 4, 5, 6]
C
[1, 2; 3, 4; 5, 6]
D
Error

QUIZZES IN Matlab