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

randomsparse

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."

Best Answer

You're mixing the usage of s, sometimes using it to refer to the sparse data and sometimes to the RandStream.