Generate a random sparse matrix with N non-zero-elements

Viewed 264

I've written a function that generates a sparse matrix of size nxd
and puts in each column 2 non-zero values.

function [M] = generateSparse(n,d)
    M = sparse(d,n);
    sz = size(M);
    nnzs = 2;
    val = ceil(rand(nnzs,n));
    inds = zeros(nnzs,d);
    for i=1:n
        ind = randperm(d,nnzs);
        inds(:,i) = ind;
    end 
    points = (1:n);
    nnzInds = zeros(nnzs,d);
    for i=1:nnzs
        nnzInd = sub2ind(sz, inds(i,:), points);
        nnzInds(i,:) =  nnzInd;
    end
    M(nnzInds) = val;
end

However, I'd like to be able to give the function another parameter num-nnz which will make it choose randomly num-nnz cells and put there 1.

I can't use sprand as it requires density and I need the number of non-zero entries to be in-dependable from the matrix size. And giving a density is basically dependable of the matrix size.

I am a bit confused on how to pick the indices and fill them... I did with a loop which is extremely costly and would appreciate help.

EDIT:
Everything has to be sparse. A big enough matrix will crash in memory if I don't do it in a sparse way.

1 Answers
Related