This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Vectors and Matrices – 1”.
1. Vectors depend upon brackets while scalars don’t.
a) True
b) False
View Answer
Explanation: To declare a scalar, we only need to declare a variable in MATLAB with a constant expression. We don’t need to include the expression with any kind of brackets. But, we need to put the expressions within a bracket for row or column vector.
2. How many errors will MATLAB show if the following code entered?
A=[1;2;3]; B=[1 2]; C=A.*B;D=C*A;
a) 2
b) 1
c) No error
d) 4
View Answer
Explanation: There are two errors here. Firstly, vector dimensions should be the same for A and B to compute the C matrix. Secondly, vector multiplication is done by the symbol ‘.*’. So, D=C*A will give an error but MATLAB as soon as it encounters the first error, it will stop compiling the code and return the first error invoked. Hence the number of errors MATLAB will show is 1.
3. To see the sub-matrix with aij for 2<=i<=4 and 1<=j<=2 of a matrix a, of order 5*6, which code is used?
a) a(2;4,1;2)
b) a(2,4:1,2)
c) a(2:4,1:2)
d) a(2,4;1,2)
View Answer
Explanation: To see the sub-matrix of a matrix a, of any order (here 5*6), the following syntax is used: matrix name(2:4,1:2). Here, ‘2:4’ mentions rows from 2 to 4 while ‘1:2’ mentions the columns from 1 to 2. This is a pre-defined form of viewing or making a sub-matrix of a matrix in MATLAB.
4. Which code shows the use of ellipsis in MATLAB?
a)
A = [1 2 3;... 5 6 7]
b) A = [1;2’3]
c)
A = [1;2.. 3;4]
d) A = [1:2..:2]
View Answer
Explanation: Ellipsis is a method of continuing lines in MATLAB to fit in a matrix if the length is very big to be in a row. The correct syntax for ellipsis while other options will generate an error of invalid input character and unexpected MATLAB operator respectively.
A = [1 2 3;... 5 6 7]
It is another form of continuing lines but that form is called carriage return.
5. What is the symbol used to evaluate the transpose of a vector?
a) “ ^ ”
b) “ * ”
c) “ ‘ ”
d) “ ~ ”
View Answer
Explanation: It is pre-defined in MATLAB that if we want to find the transpose of a vector, we will have to use the “ ‘ ” symbol following the vector or the vector name. “ ^ ” is used to raise the power of a variable while “ * ” is used for multiplication purposes. “ ~ ” is used to denote not-equal to the operator which is “ !~ ”. Hence option “ ‘ ” is correct.
6. What is the advantage of MATLAB over other computing software with matrix dimensions?
a) No advantage
b) Real time pre-defined memory allocation
c) Real time user-defined memory allocation
d) Matrix operations are easily computed
View Answer
Explanation: In many softwares, while declaring a matrix- the user has to assign a size of the matrix before entering any values to it. But in MATLAB, the user does not need to mention any size; mentioning the number of elements is enough for MATLAB to determine the size of the matrix required.
7. Which code will help to concatenate two matrices easily in 2 dimensions?
Matrices: A = 1 2 B = 1 2 3 4 3 4
a) cat(A,B)
b) cat(2,[1 2;3 4], [1 2; 3 4])
c) cat(2;[1 2,3 4]; [1 2, 3 4])
d) cat([1 2;3 4], [1 2; 3 4])
View Answer
Explanation: The syntax to concatenate two matrices along a specific dimension, d, is cat(d,A,B). Now, we don’t have to define the two matrices explicitly if we don’t need to. We can enter the matrices, within the cat(), as we declare a matrix. MATLAB would check for the syntax of matrix declaration and concatenate the matrices. To declare matrix A, the code is A=[1 2;3 4] where a “;” signifies an increase in rows- same goes for declaring B.
8. Which code can be used for changing the dimensions of a matrix as follows?
Input matrix: 1 2 3 Output matrix: 1 4 2 5 3 6 4 5 6
a) reshape([1,2,3;4,5,6],1,6)
b) reshape([1,2,3;4,5,6];1;6)
c) reshape([1,2,3;4,5,6]:1:6)
d) reshape([1,2,3;4,5,6],6,1)
View Answer
Explanation: There is a pre-defined function in MATLAB which allows the user to change the dimensions of a matrix without much to be done. The function is ‘reshape(A,row,column)’ where A is the original matrix, row denotes desired rows while column denotes desired columns. Now, the matrix A is defined in MATLAB as A=[1,2,3;4,5,6], but we don’t need to do it explicitly. We can put it directly within the function and henceforth put the desired row and columns required( here 1 and 6).
9. What is the function used to multiply a matrix, A, with itself n times?
a) mtimes(A,n)
b) ntimes(A,n)
c) mtimes(n,A)
d) mtimes(A^n)
View Answer
Explanation: The syntax of the function to multiply a matrix with itself n times is ‘mtimes(A,n); where A is the matrix itself and n is the number of times the matrix need to be multiplied to itself. There are no function ntimes() in MATLAB and the rest of the options show incorrect syntax.
10. Which code is used to solve the system of linear equation: A.(x2)= B?
A = 1 4 B = 1 2 1 4 1 2
a) sqrt([1 4;1 4]/[1 2;1 2])
b) Inf
c) sqrt([1 4,1 4]/[1 2,1 2])
d) sqrt[(1 4;1 4]/[1 2;1 2)]
View Answer
Explanation: It is seen from the equation that if we divide B by A and find the square root of the result, we will get the values of x in a vectored form. But, the matrices are singular matrices. So determinant value of both matrices is 0. Hence the output will be as follows
Output: ans= 0.0000 + Infi Inf + 0.0000i 0.0000 + Infi Inf + 0.0000i
11. Which operator set is used for left and right division respectively?
a) .\ and ./
b) ./ and .\
c) Left division and right division is not available in MATLAB
d) / and \
View Answer
Explanation: In MATLAB, if we want to perform left division of two matrices we need to write a.\b while for the right division we have to write a./b. for left division respectively.
12. If A and B are two matrices, such that a./b=b.\a, what is concluded?
a) Nothing special
b) A = AT
c) A = A-1
d) A = A
View Answer
Explanation: a./b is same as b.\a. In the former, we are performing left division so b is divided by a. In the latter, we are performing right division so b is again divided by a. Thus, it is obligatory that a./b=b.\a. Hence, nothing special can be said about the two matrices A and B.
13. If a./b=(b./a)T, what can be concluded about the matrices a and b?
a) a = bT
b) a = b-1
c) a = b’
d) nothing special
View Answer
Explanation: ‘a./b’ means that elements of a are divided b while ‘b./a’ means that the elements of b are divided by a. If the result of the latter is the transpose of the former, it suggests that a=bT. This is because element-wise division is performed by the operator ‘./’. So the resultant matrix is a direct symbolisation of the nature of occurrence of elements in either matrix. Hence option a=bT is correct.
14. What is the difference between a[] and a{}?
a) a[] is for empty cell array while a{} is for empty linear array
b) a[] is for empty linear array while a{} is for empty cell array
c) No difference
d) a[] is an empty row vector while a{} is an empty column vector
View Answer
Explanation: To initialise a cell array, named a, we use the syntax ‘a{}’. If we need to initialise a linear array, named a, we use the syntax ‘a[]’. This is pre-defined in MATLAB.
15. Choose the function to solve the following problem, symbolically, in the easiest way.
3x+y=5 and 2x+3y=7
a) linsolve(x,y) where x and y are the co-efficient and result matrix respectively
b) solve(x,y) where x and y are the co-efficient and result matrix respectively
c) sol(x,y) where x and y are the co-efficient and result matrix respectively
d) the equations are not solvable
View Answer
Explanation: ‘linsolve’ performs LU factorization method to find the solution of a system of equation AX=B. The syntax is ‘linsolve(x,y)’ where x is the co-efficient matrix while b is the result matrix. The function solve () could’ve been used, but the expressions within parentheses should be the equations, within apostrophes, declared as strings. Same for sol. Hence, option linsolve(x,y) where x and y are the co-efficient and result matrix respectively is correct.
Output: linsolve([3,1;2,3],[5;7] ans= 1.1428571428571428571428571428571 1.5714285714285714285714285714286
Sanfoundry Global Education & Learning Series – MATLAB.
To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.