1. What is a scalar in MATLAB? Give an example.
Answer:
A scalar is a single number.
Example: s = 5;
2. How do you define a row vector and a column vector?
Answer:
Row vector: a = [1 2 3];
Column vector: b = [4; 5; 6];
3. What is the output of c = 1:5?
Answer:
c = [1 2 3 4 5]; — it creates a row vector using the colon operator.
4. How do you create a 5×5 matrix of zeros in MATLAB?
Answer:
zeros(5,5);
5. Explain what eye(5) does.
Answer:
eye(5) creates a 5×5 identity matrix with 1s on the diagonal and 0s elsewhere.
6. Differentiate between rand(5,5) and randn(5,5).
Answer:
rand(5,5) generates random values from a uniform distribution between 0 and 1.
randn(5,5) generates values from a standard normal distribution (mean = 0, std = 1).
7. What does the dot (.) mean in operations like x .* y or x ./ y?
Answer:
It indicates element-wise operations.
.* is element-wise multiplication
./ is element-wise division
8. How do you find the dot product of two vectors x and y?
Answer:
Use dot(x, y);
9. Write a MATLAB command to access the element in the 2nd row and 3rd
column of matrix A.
Answer:
A(2,3);
10. What is implicit expansion? Give an example.
Answer:
Implicit expansion is when MATLAB automatically adjusts the size of arrays to make operations
possible.
Example:
matlab
CopyEdit
D = [1 1 1; 2 2 2; 3 3 3];
F = [2 4 6];
D - F;
MATLAB expands F to match D.
11. What will be the result of this code?
matlab
CopyEdit
C = [1 2 3; 1 2 3];
3 * C
Answer:
Each element of C is multiplied by 3:
CopyEdit
3 6 9
3 6 9
12. Why does the operation a - r cause an error if a is 3x3 and r is 1x2?
Answer:
Because the dimensions are incompatible for implicit expansion — MATLAB can't broadcast a
1x2 array across a 3x3 matrix.
13. How does MATLAB handle x = [1 2 3]; y = [10; 15]; x + y?
Answer:
It expands both vectors to a 2×3 matrix:
CopyEdit
11 12 13
16 17 18
14. What's the difference between g * v and g .* v?
Answer:
g * v: Matrix multiplication (follows algebra rules)
g .* v: Element-wise multiplication
15. What does d' do in MATLAB?
Answer:
It transposes the matrix d — rows become columns and vice versa.
16. If t = 2^6 and d * t is computed, what happens?
Answer:
2^6 = 64, and each element in matrix d is multiplied by 64.
17. How would you divide each element of matrix d by 64?
Answer:
l = d / 64;
18. What does disp(x) do?
Answer:
It displays the value of x without showing the variable name.
19. How can you check the size of a matrix A?
Answer:
size(A);
20. How do you get the number of elements in a vector a?
Answer:
length(a);
1. Explain the difference between a matrix and an array in MATLAB. Can they
be used interchangeably?
Answer:
In MATLAB, both matrices and arrays refer to collections of data organized in rows and
columns. A matrix is a specific kind of 2D array (having only rows and columns), while an
array is a more general term that can refer to 1D (vectors), 2D (matrices), or even n-dimensional
data structures. In practice, when dealing with numerical data, MATLAB often uses the terms
interchangeably for 2D data. However, when working with multidimensional data, the distinction
becomes important. Arrays allow for more flexibility, like 3D arrays or higher, while matrices
are strictly 2D.
2. Describe how MATLAB handles vector and matrix operations differently
from scalar operations. Why is this distinction important?
Answer:
MATLAB is designed to operate efficiently on entire vectors and matrices at once, rather than
looping through each element like traditional programming languages. When performing
operations:
Scalar operations (like x + 5) apply a single number to all elements.
Vector or matrix operations follow specific rules:
o Use operators like .*, ./, and .^ for element-wise operations.
o Use *, /, and ^ for matrix algebra operations, which require conforming
dimensions.
This distinction is important because applying the wrong operator can cause
dimension errors or unintended calculations. Understanding when MATLAB
performs element-wise vs. matrix operations is crucial for writing correct and
efficient code.
3. What is the colon operator in MATLAB, and what are three practical ways it's
used?
Answer:
The colon operator (:) is one of the most powerful tools in MATLAB. It’s used for:
1. Creating vectors: 1:5 produces [1 2 3 4 5].
2. Specifying step size: 1:2:9 gives [1 3 5 7 9].
3. Indexing: A(:,2) extracts all rows from column 2 of matrix A.
It simplifies code by eliminating the need for loops when generating sequences or
selecting data subsets. For example, instead of writing a loop to fill a vector from 1 to 10,
you can just use 1:10.
4. Explain what implicit expansion is, how it differs from broadcasting in other
languages like Python, and when MATLAB uses it.
Answer:
Implicit expansion is MATLAB’s way of handling operations between arrays of different sizes
without explicitly replicating values. It was introduced to simplify code and remove the need to
use functions like repmat.
Example:
matlab
CopyEdit
A = [1; 2; 3]; % 3x1 column
B = [10 20 30]; % 1x3 row
C = A + B;
MATLAB will automatically expand A to a 3x3 matrix by replicating its column down each row
and expand B across each column to perform the addition. In Python (NumPy), this is called
broadcasting.
Implicit expansion avoids memory waste and increases code readability. However, if dimensions
are truly incompatible (e.g. 3x2 + 1x3), MATLAB will throw an error.
5. Discuss the importance of preallocating arrays in MATLAB and the impact it
has on performance.
Answer:
Preallocating an array means defining its size before entering a loop that fills it. For instance:
matlab
CopyEdit
A = zeros(1, 1000);
for i = 1:1000
A(i) = i^2;
end
Without preallocation, MATLAB dynamically resizes the array each time a new element is
added, which is computationally expensive and slows down execution. Especially in large
programs or with large datasets, preallocating improves performance significantly and is
considered best practice in efficient MATLAB programming.
6. In what situations would you use eye(n), zeros(m,n), and ones(p,q) in
MATLAB?
Answer:
eye(n) creates an identity matrix — useful for linear algebra operations, inverting
matrices, and systems of equations.
zeros(m,n) creates an m×n matrix of all zeros — useful for preallocation or initializing
matrices when the content will be updated later.
ones(p,q) creates a p×q matrix of all ones — often used in simulations, baseline
models, or element-wise multipliers.
Each of these is fundamental in numerical computing, simulation, and algorithm development
because they allow you to control structure and initialization efficiently.
7. How does MATLAB differentiate between matrix multiplication and element-
wise multiplication? Give clear examples.
Answer:
Matrix multiplication (*) follows the rules of linear algebra — the number of columns in the first
matrix must equal the number of rows in the second.
Example:
matlab
CopyEdit
A = [1 2; 3 4];
B = [5; 6];
C = A * B; % Valid matrix multiplication
Element-wise multiplication (.*) multiplies corresponding elements of arrays of the same size.
Example:
matlab
CopyEdit
X = [1 2 3];
Y = [4 5 6];
Z = X .* Y; % Returns [4 10 18]
Understanding this difference is key to avoiding dimension errors and achieving the correct
computation.
8. Why is the transpose operation important in MATLAB, and how is it applied
differently to real and complex matrices?
Answer:
The transpose operation (') flips a matrix over its diagonal — converting rows to columns and
vice versa.
For real matrices, A' simply transposes the matrix.
For complex matrices, A' gives the conjugate transpose, where each element is also
conjugated (i.e., imaginary part sign flipped).
To perform a non-conjugate transpose on complex matrices, use A.'.
Transpose is essential in linear algebra (e.g., solving systems, finding orthogonality), data
manipulation, and when matching vector orientations in operations.
1. What is MATLAB primarily used for?
Answer:
MATLAB is used for mathematical computations, simulations, data analysis, algorithm
development, and visualization. It's especially popular in engineering, science, and applied
mathematics.
2. What symbol is used to suppress output in MATLAB?
Answer:
The semicolon ; is used to suppress output so that MATLAB doesn't display the result in the
Command Window.
3. What function is used to create a row vector from 1 to 10?
Answer:
You can use the colon operator: 1:10
4. How do you generate a 5×5 matrix of zeros in MATLAB?
Answer:
Use the zeros function: zeros(5,5)
5. What function creates a matrix with ones?
Answer:
ones(rows, columns)
Example: ones(3,4) creates a 3×4 matrix of ones.
6. How can you get the size of a matrix A in MATLAB?
Answer:
Use the size(A) function.
7. What does the command length(A) return?
Answer:
It returns the length of the longest dimension of array A.
8. How do you extract all elements of the third row in a matrix A?
Answer:
A(3,:)
9. What does A(:,2) return?
Answer:
All rows from the second column of matrix A.
10. What function is used to create an identity matrix?
Answer:
eye(n) creates an n×n identity matrix.
11. What does the transpose operator (') do to a matrix?
Answer:
It flips the matrix over its diagonal (rows become columns).
12. What is the difference between * and .* in MATLAB?
Answer:
* is matrix multiplication.
.* is element-wise multiplication.
13. Write a MATLAB command to create a vector from 0 to 10 with a step of 2.
Answer:
0:2:10
14. What does linspace(1, 10, 5) do?
Answer:
Creates a vector with 5 equally spaced values between 1 and 10:
[1 3.25 5.5 7.75 10]
15. What is the difference between disp() and omitting the semicolon?
Answer:
disp() explicitly displays the result in a clean format.
Omitting the semicolon prints the result by default but with extra detail (like variable
name).
16. How do you plot y = sin(x) in MATLAB?
Answer:
matlab
CopyEdit
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);
17. What is the purpose of the hold on command in plotting?
Answer:
It allows you to add multiple plots to the same figure without deleting the previous ones.
18. How do you add a title to a plot?
Answer:
Use the title('Your Title') function.
19. What command is used to create a horizontal bar chart?
Answer:
barh(data)
20. How do you save a figure in MATLAB?
Answer:
Use the saveas or print function. Example: saveas(gcf, 'plot1.png')
1. Explain the difference between a script and a function in MATLAB.
Answer:
A script is a file containing a sequence of MATLAB commands. It runs all commands in the
current workspace and does not accept input or return output.
A function, on the other hand, is a file that accepts inputs and returns outputs. It has its own
workspace (called a local workspace), which makes it ideal for reusable operations.
2. Describe how MATLAB handles matrix operations differently from element-
wise operations.
Answer:
In MATLAB, matrix operations such as multiplication (*), division (/), and power (^) follow
linear algebra rules, which require the matrices to be compatible in dimensions (e.g., inner
dimensions must match for multiplication).
Element-wise operations, indicated by a dot before the operator (.*, ./, .^), apply the
operation to corresponding elements of two arrays of the same size.
3. Explain the use and importance of the colon operator (:) in MATLAB.
Answer:
The colon operator is a powerful tool in MATLAB used to create vectors, select portions of
arrays, and specify ranges. For example:
1:5 creates a vector [1 2 3 4 5]
A(:,2) selects all rows of column 2 in matrix A
It simplifies code and reduces the need for loops when performing array slicing.
4. What is vectorization in MATLAB, and why is it preferred over using loops?
Answer:
Vectorization is the process of replacing explicit loops with MATLAB’s built-in vector and
matrix operations. It is preferred because MATLAB is optimized for matrix computations, so
vectorized code runs faster, is shorter, and often easier to read and debug compared to loop-
based code.
5. Differentiate between zeros, ones, and eye functions in MATLAB. Give use-
cases.
Answer:
zeros(m,n) creates an m×n matrix of zeros — used to preallocate space or represent
absence of values.
ones(m,n) creates an m×n matrix of ones — used for initializing matrices or weights.
eye(n) creates an n×n identity matrix — often used in solving systems of equations or
linear algebra operations.
6. How do subplot() and hold on enhance plotting in MATLAB?
Answer:
subplot(m,n,p) allows multiple plots in one figure by dividing the figure into an m×n
grid and placing the plot in position p. This is useful for comparing multiple plots side by
side.
hold on retains the current plot and allows additional graphics to be added without
erasing the existing plot. It's useful when plotting multiple lines on the same graph.
7. Describe how conditional statements (like if, else, elseif) work in MATLAB.
Answer:
Conditional statements allow MATLAB to execute certain sections of code based on logical
conditions:
matlab
CopyEdit
if condition
statements
elseif another_condition
other_statements
else
fallback_statements
end
They’re used for decision-making in scripts and functions based on inputs or calculations.
8. Explain the difference between for and while loops in MATLAB.
Answer:
A for loop repeats a group of statements a fixed, predetermined number of times. It’s
ideal when you know in advance how many times to run.
A while loop repeats as long as a condition is true. It’s used when the number of
iterations is not known ahead of time.
9. What are some common uses of MATLAB in real-world applications?
Answer:
MATLAB is used in various fields:
Engineering: control systems, signal processing, system modeling.
Finance: risk modeling, portfolio analysis.
Science: data visualization, simulations.
Academia: algorithm development, numerical computation.
Its ability to handle large matrices and visualize data makes it valuable for complex
computations and prototyping.
10. What are M-files in MATLAB? What types are there?
Answer:
M-files are plain text files containing MATLAB code, saved with the .m extension. There are
two types:
Script M-files: Series of MATLAB commands run in sequence.
Function M-files: Define functions that accept inputs and return outputs, used for
modular programming and reusability.