How can you delete a random fraction of entries from a sparse matrix?

a - sparse matrix f - fraction which should be removed
I tried:
nonZeros = nnz(A);
[i,j,s] = find(A);
s = RandStream('mt19937ar','Seed',0);
selection = randperm(s,floor((1-f/100) * nonZeros));
i = i(selection);
j = j(selection);
s = s(selection);
A = sparse(i,j,s);
On line "i = i(selection);" I get the error message: "You cannot index into a RandStream using () indexing."

 Accepted Answer

Matt J
Matt J on 2 Dec 2013
Edited: Matt J on 2 Dec 2013
You're mixing the usage of s, sometimes using it to refer to the sparse data and sometimes to the RandStream.

1 Comment

I don't think you need randperm:
[i,j,s] = find(A);
numkeep=floor((1-f/100) * length(s));
selection = randi(length(s),1,numkeep);
i = i(selection);
j = j(selection);
s = s(selection);
A = sparse(i,j,s);

Sign in to comment.

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Asked:

on 2 Dec 2013

Commented:

on 2 Dec 2013

Community Treasure Hunt

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

Start Hunting!