MATLAB Questions and Answers – Sparse Matrices – 1

«
»

This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Sparse Matrices – 1”.

1. What is the attribute of sparse matrices?
a) sparse
b) double
c) vector
d) no attribute
View Answer

Answer: a
Explanation: The attribute of a sparse matrix will be sparse while the class of the attribute is double, by default. Hence, sparse is correct.
advertisement

2. What is the output of the following code?

sparse[m n]

a) A m*n all zero sparse matrix
b) A m*n sparse matrix
c) Error due to syntax
d) Error in the input
View Answer

Answer: c
Explanation: There are two errors in the above code. MATLAB would return the syntactical error due to [] since there should be a parenthesis. There is an error in the input since there should be a ‘ , ‘ between m an n.

3. The nature of complex input taken by the sparse() command is ______________
a) Only Imaginary part
b) Only positive imaginary part
c) Only negative real part
d) All of the mentioned
View Answer

Answer: d
Explanation: The sparse() command takes all kind of complex inputs. It is not biased by default.

4. Which of the following can be the space taken up by a sparse matrix?
a) .25 megabytes
b) 600 megabytes
c) .5 GB
d) 450 megabytes
View Answer

Answer: a
Explanation: The sparse matrix takes extremely small space from memory which is why it is so useful. The most plausible option amongst is .25 megabytes while the rest suggest pretty large amount of memory.

5. To check whether the input matrix is sparse or not, we use the ________ command.
a) issparse
b) besparse
c) ifsparse
d) sparse
View Answer

Answer: a
Explanation: The correct command to check whether the input matrix is sparse or not is the issparse command. The sparse command generates a sparse matrix.
advertisement

6. A sparse identity matrix is generated by the ______ command.
a) sparseid
b) isparse
c) speye
d) idensparse
View Answer

Answer: a
Explanation: The correct command to generate a sparse identity matrix is the speye command. The issparse command checks whether an input matrix is a sparse matrix. Hence speye is correct.

7. What is the command used to generate a sparse normally generated matrix?
a) sparserndn
b) sprandom
c) sprandn
d) no such command
View Answer

Answer: c
Explanation: The sprandn command uses the same random number generator as that of the randn command. The rest of the commands don’t exist.

8. What is the output of the following code?

A=[1 2 0 3; 2 8 4 1; sin(Inf) 2 3 4];
P=sparse(A); nnz(P)

a) 11
b) 10
c) Error while declaring A
d) Error while making sparse matrix
View Answer

Answer: a
Explanation: The number of non-zero numbers in the A vector is 11. Sin(Inf) Is treated as NaN and holding a non-zero value since the range of sine is [-1,1]. There won’t be any error while declaring A or making the sparse matrix.

9. The non-zero elements in a sparse matrix are shown by the ______ command.
a) nzeros
b) nonzeros
c) notzeros
d) nozero
View Answer

Answer: b
Explanation: The non-zeros command is used to get the non-zero elements present in the sparse matrix. The rest of the options are not defined in MATLAB.

10. What is the output of the following code?

advertisement
A=[0 Inf/Inf 0 0; 2 9 7 0; sin(Inf) 8 0 0];
P=sparse(A);q=nmz(p); L=full(P);

a) l = a
b) lmemory > amemory
c) lmemory < amemory
d) lmemory != amemory
View Answer

Answer: a
Explanation: The full command converts a sparse matrix into it’s original matrix. The space taken up by both the matrices in the memory are same. Hence, only l=a is correct.

11. The size of the sparse matrix will be ___ the original matrix.
a) equal
b) greater than
c) less than
d) not equal to
View Answer

Answer: a
Explanation: The sparse matrix stores the non-zero elements in the sparse matrix. The space taken up by the sparse matrix being very less than the original, the size of both the matrix will be same. Hence, only option equal is correct.

12. The maximum space allocated for sparse matrices is given by the ____ command.
a) maxsparse
b) sparsemax
c) nzmax
d) no such command
View Answer

Answer: c
Explanation: The nzmax is the command to find the maximum space allocated for sparse matrices. The maximum space is proportional to the number of non-zero elements in the original matrix.

13. The output of the following command is

a=[1 2 3;4 0 0;3 0 9]; spy(A)

a) a graph of sparsity
b) a pattern of sparsity
c) syntactical error
d) logical error
View Answer

Answer: a
Explanation: The spy command generates the sparsity pattern of the sparse matrix which might be generated from the given matrix. The output will generate a pattern in a window which shows the position of non-zero elements occupying space.
Output: matlab-questions-answers-sparse-matrices-1-q13

14. The output of the following code is:

advertisement
a=[pi/2 pi 3*pi]; spy[a]

a) Suppressed output
b) A pattern of sparsity
c) Syntactical error
d) Symbolic error
View Answer

Answer: c
Explanation: The input to the spy command has to be within parentheses. Due to this, there will be a syntactical error in the above command.

15. The spy command takes in multiple matrices.
a) True
b) False
View Answer

Answer: b
Explanation: The spy command does not take in multiple inputs. It will show the sparsity pattern of only input vector.

Sanfoundry Global Education & Learning Series – MATLAB.

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn