Fast way to retrieve nonzero entries of each row in a sparse matrix

19 Ansichten (letzte 30 Tage)
I'm working with very large sparse matrices (10's of thousands to millions of entries) and I'd like to efficiently retrieve and store the unique nonzero entries of each row. For my application, the rows will have anywhere between 1 and 9 unique and nonzero entries.
I've decided to utilize "sprand" for an example - I'm fairly new to using sparse matrices and am not the best programmer so forgive my naivety - and I've tried to choose a density within the aforementioned range. My code is:
numrows = 1000000;
numcols = 1000000;
density = numrows^(-1.87);
num_entries_per_row = numrows*numcols*density % This is an average.
rsm = sprand(numrows,numcols,density); % random sparse matrix
out = cell(numrows,1); % output.
for idx = 1:numrows
[~,~,value] = find(rsm(idx,:));
out{idx} = unique(value);
I've had this running for a few minutes and still no result. Previously when the number of rows and columns were both 100,000 the result took about 32 seconds - way too slow for my purposes. Clearly not the fastest, but it is simple. Is there a better and more clever way to achieve this result in MATLAB?
  2 Kommentare
Dominik Mattioli
Dominik Mattioli am 31 Aug. 2017
Can you double check your density equation? It seems to produce a very small number compared to what you seem to be working with.
>> I set the density equation to give me a num_entries (per row) that is approximately equal to 6, which is a good number for me. Sorry for the confusion, I will edit the variable name in my question.
Also, do the resulting values need to retain any order or can they be sorted?
>> No order necessary, as long as they are unique.
And can you build and run mex routines?
>> I have zero experience with other languages, so unfortunately I cannot know how to build these.
The idea would be to transpose the matrix, then sort it, then have a mex routine do the unique calculation by column. Result would be a single matrix with the unique row data contained in the columns of the result.
>> This sounds simple enough. Do you think it is more efficient than Matt J's answer?
Are you sure you want a cell array containing millions of elements?
>> Is there a better storage method considering the rows of rsm will have varying numbers of unique nonzero entries?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 31 Aug. 2017
Bearbeitet: Matt J am 31 Aug. 2017
  5 Kommentare
Matt J
Matt J am 31 Aug. 2017
Bearbeitet: Matt J am 31 Aug. 2017
keeping the result in a sparse matrix rather than millions of cell elements
With further testing, I'm actually seeing that cells overtake matrices in efficiency when the density of rsm gets above a certain threshold. Not sure why...
numrows = 1000000;
numcols = 1000000;
rsm = sprand(numrows,numcols,30/numcols);
%Elapsed time is 8.225773 seconds.
%Elapsed time is 16.365144 seconds.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)


Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by