MATLAB Multiple Choice Questions & Answers on “Sparse Matrices – 2”.
1. What is the output of the following code?
A=[0 0 0; 0 9 0; 1 2 3]; nnz[A]
a) 4
b) 5
c) 3
d) Error
Answer: d
Clarification: There is syntactical error while giving input to the nnz command. The input to the nnz command should be within parentheses and here the input is within []. If the input was given within (), the output would’ve been 4.
2. What is the output of the following code?
A=[1 2 3; 32 23 26; 0 0 0]; spones(A)
a) Returns a sparse matrix with the non-zeros replaced by normally distributed random numbers
b) Returns a sparse matrix with the zeros replaced by ones
c) Returns a sparse matrix with the non-zeros replaced by fractions
d) Returns a sparse matrix with the non-zeros replaced by random numbers
Answer: b
Clarification: The spones command returns a sparse matrix with ones replaced by zeros. Hence, Returns a sparse matrix with the zeros replaced by ones is correct while rest of the options are incorrect.
Output:
ans =
(1,1) 1
(2,1) 1
(1,2) 1
(2,2) 1
(1,3) 1
(2,3) 1
3. The space located for the matrix generated from the spones command is _______
a) Same as a sparse matrix
b) Same as the original matrix
c) Same as an identity matrix
d) Double that of the sparse matrix
Answer: a
Clarification: The spones command also creates a sparse matrix. Hence, the newly generated matrix will have the same size as that of a sparse matrix.
4. What is the output of the following code?
A=[1 2 3; 4 5 6; 7 8 9];if( nzmax(A)==nzmax(spones(A) ) disp(‘Yeah !’)
a) No output
b) Error
c) Yeah !
d) Output suppressed
Answer: a
Clarification: There is no error in the above code. Since, the if command is not ended, no output will be generated. If the if command was ended, the output would’ve been Yeah !.
5. What is the output of the following code?
nnz(spconvert([1 2 3; 4 5 6; 7 8 9])
a) 3
b) 2
c) 1
d) 6
Answer: a
Clarification: The spconvert command converts the original matrix into a sparse matrix by only keeping memory for the elements in the columns. Since in the above matrix, there are no non-zero elements- the total space occupied by the new matrix is 3.
Output: 3
6. What is the output of the following code?
nzmax(spconvert([1 2 3; 4 5 6; 7 8 9])
a) 2
b) 3
c) 1
d) Error
Answer: b
Clarification: The matrix generated by the spconvert command consists of non-zero elements from the columns of the original matrix. Hence, 3 is correct.
Output: 3
7. A memory for sparse matrix is dedicated by the ______ command.
a) spalloc
b) sparsealloc
c) allocspar
d) no such command
Answer: a
Clarification: The spalloc command is used to allocate memory for a sparse matrix. The rest of the commands are incorrect.
8. What is the output of the following command?
a) A 2*3 sparse matrix
b) Memory is allocated for a 2*3 sparse matrix
c) A 3*2 sparse matrix
d) Error
Answer: b
Clarification: The spalloc command will allocate memory for 2*3 matrix due to the above code. There will be no error.
9. The default number of non-zero elements which can be put into the memory allocated by the spalloc command is > 1.
a) True
b) False
