MATLAB Functions and Commands –
Function / Explanation Example Result Condition
Command
A+B Performs A=[1 2;3 4]; B=[5 6;7 C = [6 8; 10 12] A and B
matrix 8]; C=A+B; must be the
addition. same size.
A-B Subtracts A=[4 5;6 7]; B=[1 2;3 C = [3 3; 3 3] A and B
matrix B 4]; C=A-B; must be the
from A. same size.
A*B Performs A=[1 2;3 4]; B=[5;6]; C = [17; 39] Number of
matrix C=A*B; columns in
multiplicatio A = rows in
n. B.
A\B Solves A*x = A=[2 3;4 5]; B=[8;14]; X = [1; 2] A must be
B using X=A\B; square and
Gaussian non-
elimination. singular.
A' Returns the A=[1 2 3;4 5 6]; B=A'; B = [1 4; 2 5; 3 6] Applicable
transpose of to any
matrix A. matrix.
axis Sets axis plot(1:10); axis([0 12 Adjusts plot to Used only in
limits for a 0 15]); show x:0–12, y:0– plots.
plot. 15
clear Removes clear x y; x and y deleted Variables
variables from workspace. must exist.
from
workspace.
clear all Clears all clear all; or clear; Workspace fully Removes all
variables and cleared. loaded data.
functions.
clc Clears clc; Clears all text in No
command command window. condition.
window.
countdown Creates countdown=5:-1:0; countdown=[5 4 3 Valid
and numeric countup=0:2:10; 2 1 0]; countup=[0 syntax:
countup sequences. 2 4 6 8 10] start:step:en
d
cos Computes cos(pi/3); 0.5 Angle input
cosine in in radians.
radians.
sin Computes sin(pi/2); 1 Input in
sine of an radians.
angle in
radians.
tan Computes tan(pi/4); 1 Angle in
tangent of an radians.
angle in
radians.
det Computes A=[2 3;1 4]; det(A); 5 Matrix must
determinant be square.
of a square
matrix.
disp Displays text x=25; disp(x); Displays: 25 No
or variable condition.
value.
exp Calculates exp(1); 2.7183 Input must
exponential be numeric.
function e^x.
figure Creates a figure; plot(1:10); Opens a new plot Used only
new figure window. for graphics.
window.
format Sets numeric format long; pi; 3.1415926535897 Affects
long/short display 93 display only,
precision. format long; pi; 3.1415 not value.
fprintf Displays fprintf('Hello\n'); Prints: “Hello” then Text
formatted new line formatting
text. required.
fprintf('tex Displays x=12.3456; Value=12.345600 Variable
t %f') formatted fprintf('Value=%f\n',x then new line must exist.
floating-point );
value.
fprintf('%d Displays x=10; Integer:10 then Value
') integer fprintf('Integer:%d\n', new line should be
values. x); integer.
fprintf('%f' Displays y=2.71828; 2.718280 then Value
) floating-point fprintf('%f\n',y); new line should be
value. numeric.
grid on/off Turns grid plot(1:5); grid on; Grid displayed on Used only in
lines on or plot. plots.
off.
help Displays help plot; Shows usage and Function
documentati syntax of plot. must exist.
on for a
function.
inv Computes A=[2 1;7 4]; inv(A); [4 -1; -7 2]/(1) Matrix must
matrix be square
inverse. and non-
singular.
legend Adds a plot(1:10); Legend 'Data' Used in
legend to a legend('Data'); shown on plot. plots.
plot.
load Loads load [Link]; Loads variables File must
variables into workspace. exist.
from a .mat
file.
log Computes log(10); 2.3026 Input must
natural be positive.
logarithm
(ln).
max Finds max([4 7 1 9]); 9 Input must
maximum be numeric.
element.
mean Computes mean([1 2 3 4 5]); 3 Input must
average be numeric.
value.
min Finds min([8 3 5 1]); 1 Input must
minimum be numeric.
element.
plot Creates a line x=0:0.1:2*pi; y=sin(x); Displays sine wave x and y must
plot. plot(x,y); plot. be same
length.
plot(X,Y,'k* Customizes plot(X,Y,'k*-'); Black line with star Used in
-') line style and markers. plots.
color.
save Saves save [Link]; Creates Write
variables to a '[Link]' file. permission
.mat file. required.
semicolon Suppresses x=10; y=5; z=x+y; Suppresses Syntax
(;) output or intermediate feature.
separates display.
commands.
size Returns A=[1 2 3;4 5 6]; [2 3], 2 rows and 3 Valid for
dimensions size(A); columns arrays and
of an array. matrices.
sort Sorts sort([5 1 3 2]); [1 2 3 5] Input must
elements in be numeric.
ascending
order.
sqrt Computes sqrt(25); 5 Input must
square root. be non-
negative.
sum Sums all sum([1 2 3 4]); 10 Input must
array be numeric.
elements.
title Adds title to plot(1:10); Displays title Used in
plot. title('Data'); above graph. plots.
xlabel Labels x-axis xlabel('Time (s)'); Adds label 'Time Used in
of a plot. (s)' plots.
ylabel Labels y-axis ylabel('Amplitude'); Adds label Used in
of a plot. 'Amplitude' plots.
𝑎𝑎 𝑏𝑏 1 𝑑𝑑 −𝑏𝑏
𝐴𝐴 = � � → det(𝐴𝐴) = 𝑎𝑎𝑎𝑎 − 𝑏𝑏𝑏𝑏 → 𝑖𝑖𝑖𝑖𝑖𝑖(𝐴𝐴) = � �
𝑐𝑐 𝑑𝑑 det(𝐴𝐴) −𝑐𝑐 𝑎𝑎
You should know A+B, A-B, A*B, A.*B, A/B, A./B
MATLAB MCQs –
Multiple Choice Questions
1. Which command correctly sets x-limits to 0 .. 10 and y-limits to -1 .. 1 on an existing plot?
A. axis([0 10 -1 1])
B. axis(0,10,-1,1)
C. setAxis(0,10,-1,1)
D. axes([0 10 -1 1])
2. Which creates a new figure window before plotting?
A. newfigure(); plot(x,y)
B. figure; plot(x,y)
C. fig(); plot(x,y)
D. createFigure plot(x,y)
3. Which correctly plots Y versus X?
A. plot(y = x)
B. plot(X,Y)
C. plot(X:Y)
D. plot('X','Y')
4. Which correctly adds a title and axis labels?
A. Title("My Plot"); Xlabel('t'); YLabel('f(t)')
B. title('My Plot'); xlabel('Time (s)'); ylabel('Amplitude')
C. title(My Plot); xlabel(Time); ylabel(Amplitude)
D. TITLE('My Plot'); X_LABEL('Time'); Y_LABEL('Amplitude')
5. Which correctly adds a legend entry for one data series?
A. legend = 'Data'
B. addlegend('Data')
C. legend('Data')
D. Legend('Data')
6. Which command creates a countdown vector 5,4,3,2,1,0?
A. 5-1:0
B. 5:-1:0
C. 5::1:0
D. [5..0]
7. Which is NOT a valid countdown expression to zero?
A. 10:-2:0
B. 3:-1:0
C. 7:-0:0
D. 8:-4:0
8. Which command creates an even step count-up 0,2,4,...,100?
A. 0:2:100
B. 0::2:100
C. 0:2;100
D. [0:2]-100
9. Which is NOT a valid count-up expression?
A. -4:2:6
B. 0:0:10
C. 1:3:10
D. 0:5:20
10. For a square matrix A, which returns the determinant?
A. det A
B. det(A)
C. det{A}
D. determinant(A)
11. Which statement about det(A) is TRUE?
A. det(A) is defined for any rectangular matrix
B. det(A) requires A to be square
C. det(A) equals sum(A)
D. det(A) equals trace(A)
12. Which correctly prints an integer N followed by newline?
A. fprintf('N=%d\n', N)
B. fprintf('N=%f\n', N)
C. fprintf('N= %d %d\n', N)
D. fprintf(N)
13. Which format string correctly prints a floating value with two decimals?
A. fprintf('x=%.2f\n', x)
B. fprintf('x=%2.f\n', x)
C. fprintf('x=%0.2\n', x)
D. fprintf('x=%2.2\n', x)
14. Which call is INVALID due to missing arguments?
A. fprintf('x=%f y=%d\n', x, y)
B. fprintf('value=%f\n', v)
C. fprintf('x=%f y=%d\n', x)
D. fprintf('done\n')
15. Which call is INVALID due to type mismatch?
A. fprintf('%d\n', 7)
B. fprintf('%f\n', 3.14)
C. fprintf('%s\n', 'text')
D. fprintf('%f\n', 'text')
16. Which finds the maximum element of vector v?
A. max(v)
B. maximum(v)
C. vmax(v)
D. max{v}
17. Which returns the mean of vector a?
A. mean(a)
B. avg(a)
C. mean{a}
D. MEAN(a)
18. Which returns the sum of all elements in x?
A. SUM(x)
B. sum(x)
C. sigma(x)
D. add(x)
19. Which correctly sorts vector x in ascending order?
A. sort[x]
B. sort(x)
C. sort{x}
D. sort = (x)
20. Which statement about min(x) is TRUE?
A. min(x) returns [m,idx] only
B. min(x) returns the smallest value
C. min(x) sorts x
D. min(x) returns the largest value
21. Which command clears the Command Window text?
A. clear
B. clc
C. reset
D. wipe
22. Which correctly removes variables a and b from workspace?
A. clear a b
B. clc a b
C. delete a b
D. remove a,b
23. Which correctly saves the workspace variables to [Link]?
A. save [Link]
B. save('[Link]')
C. save mydata,mat
D. A and B
24. Which loads variables from file '[Link]'?
A. load [Link]
B. load('[Link]')
C. load data,mat
D. A and B
25. Which sets numeric display to 15-digit precision?
A. format precise
B. format long
C. format short
D. format 15
Let the matrices be defined as follows:
A = [1 2 3; 4 5 6]
B = [7 8 9; 10 11 12]
C = [1 2; 3 4]
D = [5; 6; 7]
26. Which of the following is a valid matrix addition operation?
A. A + B
B. A + C
C. B + D
D. C + D
27. Which operation is INVALID due to size mismatch?
A. A + B
B. B - A
C. A + C
D. A - B
28. Which of the following matrix multiplications is VALID?
A. A * B
B. A * C
C. B * D
D. C * D
29. Which multiplication produces a 2x1 result?
A. A * D
B. B * D
C. D * A
D. C * A
30. Which operation is INVALID due to column mismatch?
A. A * D
B. C * D
C. D * C
D. A * C
31. What is the result size of A * C if A = [1 2 3;4 5 6] and C = [1 2;3 4;5 6]?
A. 2x2
B. 2x3
C. 3x3
D. Invalid
32. Which of the following scalar operations is valid?
A. A + 2
B. A * [2 3]
C. B + [1;2;3]
D. C - [1 2 3]
33. Which of the following expressions is INVALID in MATLAB?
A. A - B
B. A .* B
C. A / B
D. A + B
34. Which element-wise operation correctly multiplies A and B element by element?
A. A .* B
B. A * B
C. A .+ B
D. A ./ B
35. Which statement is TRUE about D' * D?
A. D' * D gives a 3x3 matrix
B. D' * D gives a 1x1 scalar
C. D' * D invalid
D. D' * D gives 3x1 vector
36. What is the result of 5 + 8 in MATLAB?
A. 12
B. 13
C. 14
D. 15
37. Evaluate: 10 - 4 * 2
A. 12
B. 8
C. 2
D. 6
38. What is the output of 3 * (4 + 2)?
A. 18
B. 12
C. 24
D. 6
39. Evaluate 16 / 4 + 2
A. 2
B. 4
C. 6
D. 8
40. What is the result of 2 ^ 3 * 2?
A. 8
B. 16
C. 10
D. 12
41. If A = [1 2; 3 4] and B = [5 6; 7 8], what is A + B?
A. [6 8; 10 12]
B. [4 6; 8 10]
C. [5 6; 7 8]
D. [1 2; 3 4]
42. If A = [2 3; 4 5] and B = [1 2; 3 4], what is A - B?
A. [1 1; 1 1]
B. [1 2; 1 1]
C. [2 1; 1 1]
D. [1 1; 2 2]
43. For A = [1 2; 3 4] and B = [2 0; 1 2], what is A * B?
A. [4 4; 10 8]
B. [3 2; 7 8]
C. [2 2; 3 4]
D. Invalid dimensions
44. If A = [4 2; 6 3] and B = [2 1; 3 2], what is A ./ B?
A. [2 2; 2 1.5]
B. [1 2; 2 3]
C. [2 1; 2 1.5]
D. Invalid operation
45. If A = [1 2; 3 4], what is A ^ 2?
A. [1 4; 9 16]
B. [7 10; 15 22]
C. [2 4; 6 8]
D. [10 14; 18 22]
Answer Key with Explanations
1. A – axis takes a vector [xmin xmax ymin ymax].
2. B – Use figure to open a new window.
3. B – plot(X,Y) expects numeric vectors of same length.
4. B – Function names are lowercase; text arguments in quotes.
5. C – legend('...') adds legend text.
6. B – start:step:end syntax; step is -1.
7. C – A step of -0 is invalid; step cannot be zero.
8. A – start:step:end with step 2.
9. B – Step cannot be zero.
10. B – Function-call syntax det(A) is required.
11. B – Determinant is defined only for square matrices.
12. A – %d is integer format; newline is \n.
13. A – Use %.2f for two digits after decimal.
14. C – Two conversions require two values.
15. D – %f expects numeric, not a char array/string.
16. A – Use max(v).
17. A – mean(a) computes average.
18. B – sum(x) sums elements.
19. B – Use sort(x).
20. B – min(x) returns the minimum; two outputs optional.
21. B – clc clears the Command Window; clear clears variables.
22. A – Use clear with variable names.
23. D – Both syntaxes A and B are valid; comma in C is invalid.
24. D – Both A and B valid; comma in C is invalid.
25. B – format long shows about 15 digits.
26. A – A and B are both 2x3, so A + B is valid.
27. C – A is 2x3, C is 2x2; dimensions differ.
28. B – A (2x3) * C (3x2) is valid and results in 2x2.
29. B – B (2x3) * D (3x1) → 2x1 result.
30. B – C (2x2) * D (3x1) invalid because 2≠3.
31. A – A (2x3) * C (3x2) results in 2x2 matrix.
32. A – Scalar addition is valid and adds 2 to every element of A.
33. C – Matrix division A/B invalid unless B is square (B is 2x3).
34. A – .* performs element-wise multiplication.
35. B – D' (1x3) * D (3x1) results in 1x1 scalar equal to dot product.
36. B – 5 + 8 = 13
37. D – Order of operations: 10 - (4*2) = 2
38. A – 3 * (6) = 18
39. C – 16/4 = 4, then 4+2 = 6
40. B – Exponent first: 2^3 = 8; 8*2 = 16
41. A – Addition is element-wise: [1+5 2+6; 3+7 4+8] = [6 8; 10 12].
42. A – Subtract corresponding elements: [2-1 3-2; 4-3 5-4] = [1 1; 1 1].
43. A – Matrix multiplication: [1*2+2*1 1*0+2*2; 3*2+4*1 3*0+4*2] = [4 4; 10 8].
44. C – Element-wise division: [4/2 2/1; 6/3 3/2] = [2 2; 2 1.5].
45. B – Matrix power (A*A): [1*1+2*3 1*2+2*4; 3*1+4*3 3*2+4*4] = [7 10; 15 22].