MATLAB Answers

How to create a sparse matrix with such specifications?

20 views (last 30 days)
Shweta
Shweta on 9 Aug 2013
I have to create a sparse matrix whose support is chosen uniformly at random and whose non-zero entries are independent and uniformly distributed in the range [-50,50].
Please help me out...

Answers (1)

Richard Brown
Richard Brown on 9 Aug 2013
Edited: Richard Brown on 11 Aug 2013
edit This version is wrong, but leaving so comment below makes sense
density = 0.01; % For example
m = 10000; n = 10000;
100 * sprand(m, n, density) - 50;
edit This one is better! Basically the same as what James wrote
density = 0.01;
m = 10000; n = 10000;
A = sprand(m, n, density);
A = 100*A - 50*spones(A);
  2 Comments
Richard Brown
Richard Brown on 11 Aug 2013
haha, yes, good point! I'm too used to working in C and just looping through the nonzeros. Edited answer accordingly :)

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by