MATLAB Basic Introduction 3 — Questions and Answers
Question 1: In MATLAB, what value does the built-in constant 'pi' represent?
- 3.14
- 3.14159265358979... (Correct answer)
- 22/7
- The integer 3
Correct answer: 3.14159265358979...
pi is a built-in MATLAB constant holding the double-precision value of π ≈ 3.14159265358979.
Question 2: Which command removes all variables from the MATLAB workspace?
- clc
- clear all (Correct answer)
- delete vars
- flush
Correct answer: clear all
clear all removes all variables and functions from the workspace memory.
Question 3: What is the output of size([1 2 3; 4 5 6]) in MATLAB?
- 6
- [2 3] (Correct answer)
- [3 2]
- 2
Correct answer: [2 3]
size() returns a row vector [rows cols]; the 2×3 matrix yields [2 3].
Question 4: How do you concatenate two row vectors A = [1 2] and B = [3 4] horizontally in MATLAB?
- [A; B]
- [A, B] (Correct answer)
- horzcat(A; B)
- append(A, B)
Correct answer: [A, B]
Using a comma or space inside brackets [A, B] concatenates vectors horizontally to give [1 2 3 4].
Question 5: What does the 'end' keyword refer to when used as an index in MATLAB?
- The last element of the array dimension (Correct answer)
- The end of a loop
- The termination of a function
- Index zero
Correct answer: The last element of the array dimension
When used in indexing, end refers to the last index of the dimension being accessed.
Question 6: Which MATLAB function returns the number of elements in an array?
- size()
- length()
- numel() (Correct answer)
- count()
Correct answer: numel()
numel() returns the total number of elements in an array, equivalent to prod(size(A)).
Question 7: What type of variable does MATLAB use by default for numeric values?
- int32
- float
- double (Correct answer)
- single
Correct answer: double
MATLAB stores numeric values as double-precision (64-bit) floating-point by default.
In MATLAB, what value does the built-in constant 'pi' represent?