0

I want to write a sparse matrix to a text file. Let's say my sparse matrix is A.The first row of A has non zero values at 10,11th index. The second row has non zero values at 1,2nd index. Then when I write the data to a text file it should look something like this

10 11
1 2
......
....

How can I do this in MATLAB?

chappjc
  • 30,359
  • 6
  • 75
  • 132
user34790
  • 2,020
  • 7
  • 30
  • 37

2 Answers2

0

First post, so not sure if posting code is allowed, but maybe this is what you're looking for?

[rows, cols] = size(A);
outFile = fopen('sparse.txt', 'a');
for m = 1:rows
    n = 1;
    while n <= cols
        if A(m, n) ~= 0
            fprintf(outFile, '%d ', A(m, n));
        end
        n = n+1;
    end
    fprintf(outFile, '\r\n');
end
  • This is really inefficient. My data is of size 2 million by 100 thousand. I cannot loop over each row – user34790 Feb 23 '14 at 18:58
  • @user34790 How's this then? http://stackoverflow.com/questions/217852/how-can-i-save-a-very-large-matlab-sparse-matrix-to-a-text-file –  Feb 23 '14 at 20:04
0

Given the constraints, I only really see one sensible option for the given format:

f = fopen('out.txt', 'w');
for ii=1:size(mat, 1)
    fprintf(f, '%u ', find(mat(ii, :));
    fprintf(f, '\n');
end
fclose(f);

Since the number of elements per row isn't constant, this means two things:

  • We can't construct a single matrix to pass to a vectorised function, so we're stuck with some form of per-row operation.
  • We also can't give fprintf a constant format string to write one or more whole rows in a single call, so we're stuck with multiple fprintf calls.

So, optimising for those constraints;

  • Iterate over the rows directly - yes, we could pull out the row and column indices all at once with find, but then we've wasted memory effectively copying the entire dataset, and we'd still have to iterate over that somehow.
  • Minimise the amount of high-level work - let the low-level internals of find and vectorised fprintf at least make processing a single row as quick as feasibly possible.

Sometimes a simple loop really is the best option - it's liable to be I/O-bound anyway, so the overhead of the loop itself really should be negligible compared to even the minimal number of fprintf calls.

Notlikethat
  • 20,095
  • 3
  • 40
  • 77