convert sparse matrix form matlab format into ijv coordinate format

I have sparse matrices in matlab format and I want to convert them into ijv coordinate format sorted by row(the first column) . Below is my attempt but it is not so efficient because the matrices are very large(sometimes nnz > 1M) and this code requires a lot of memory. Any better idea is highly appreciated.
function printIJV(file_name,S)
[i,j,v] = find(S);
temp2 = [i,j,v];
temp = sortrows(temp2 ,temp2(1) );
file_id = fopen(file_name,'wt');
% header: n-rows n-columns n-values
fprintf(file_id,'%d %d %d\n', size(S,1) ,size(S,2),size(v,1) );
n = size(temp , 1);
for i=1:n
fprintf(file_id,'%d %d %g\n',temp(i,1)-1 , temp(i,2)-1 , temp(i,3));
end
fclose(file_id);
end

Antworten (0)

Kategorien

Mehr zu Sparse Matrices finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 31 Mär. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by