MATLAB: sparse matrices in matlab anamoly?

  • Context: MATLAB 
  • Thread starter Thread starter manjuvenamma
  • Start date Start date
  • Tags Tags
    Matlab Matrices
Click For Summary
SUMMARY

The discussion clarifies the difference between the MATLAB functions sparse(rand(4)) and sprand(4). The sprand(4) function generates a sparse matrix with random non-zero entries, while sparse(rand(4)) converts a full matrix generated by rand(4) into a sparse matrix. Since the output of rand(4) contains no zero values, the sparse function does not reduce memory allocation effectively. Understanding the definition and purpose of sparse matrices is crucial for proper usage.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Knowledge of matrix types, specifically sparse matrices
  • Familiarity with the rand and sprand functions in MATLAB
  • Basic concepts of memory allocation in programming
NEXT STEPS
  • Explore the MATLAB documentation on sparse matrices for advanced usage
  • Learn about the sprand function and its parameters in MATLAB
  • Investigate memory optimization techniques for large datasets in MATLAB
  • Experiment with generating matrices using rand and converting them to sparse format
USEFUL FOR

MATLAB users, data scientists, and engineers working with large datasets who need to optimize memory usage through sparse matrix representation.

manjuvenamma
Messages
102
Reaction score
0
In MATLAB, Why is sparse(rand(4)) not same as sprand(4)? Is it not supposed to be? What is the reason? Please see the interaction in MATLAB pasted below.

sprand(4)

ans =

(1,1) 0.8147

>> rand(4)

ans =

0.9058 0.0975 0.9649 0.4854
0.1270 0.2785 0.1576 0.8003
0.9134 0.5469 0.9706 0.1419
0.6324 0.9575 0.9572 0.4218

>> sparse(ans)

ans =

(1,1) 0.9058
(2,1) 0.1270
(3,1) 0.9134
(4,1) 0.6324
(1,2) 0.0975
(2,2) 0.2785
(3,2) 0.5469
(4,2) 0.9575
(1,3) 0.9649
(2,3) 0.1576
(3,3) 0.9706
(4,3) 0.9572
(1,4) 0.4854
(2,4) 0.8003
(3,4) 0.1419
(4,4) 0.4218
 
Physics news on Phys.org
You haven't understood the meaning of sparse matrices. Sparse matrices store only those values that are NOT zero. Thereby, they decrease memory allocation. Your rand(4) function call didn't return any 0 value, so using sparse function on the answer has no affect.
https://in.mathworks.com/help/matlab/ref/sparse.html
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
11K