MATLAB Questions and Answers – Sparse Matrices – 2

This set of MATLAB Multiple Choice Questions & Answers focuses 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
View Answer

Answer: d
Explanation: 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.
advertisement
advertisement

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
View Answer

Answer: b
Explanation: 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
Note: Join free Sanfoundry classes at Telegram or Youtube

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
View Answer

Answer: a
Explanation: The spones command also creates a sparse matrix. Hence, the newly generated matrix will have the same size as that of a sparse matrix.
advertisement

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
View Answer

Answer: a
Explanation: 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 !.
advertisement

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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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?

spalloc(2,3, 7)

a) A 2*3 sparse matrix
b) Memory is allocated for a 2*3 sparse matrix
c) A 3*2 sparse matrix
d) Error
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: Even only 1 element can be put into the memory which has been already allocated by the spalloc command. Hence, the above statement is false.

10. The pattern generated by the spy command is a measure of the number of zeros in the input matrix.
a) True
b) False
View Answer

Answer: b
Explanation: It is actually a pattern of the number of non-zero elements in the input matrix. Hence, the above statement is false.

Sanfoundry Global Education & Learning Series – MATLAB.

To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.