Matlab from text file to sparse matrix.

I have a huge text file in the following format:

1   2
1   3
1   10
1   11
1   20
1   376
1   665255
2   4
2   126
2   134
2   242
2   247

      

The first column is the coordinate x

and the second column is the coordinate y

. He points out that if I had to build a matrix

M = zeros(N, N);
M(1, 2) = 1;
M(1, 3) = 1;
.
.
M(2, 247) = 1;

      

This text file is huge and cannot be transferred directly to main memory. I have to read this line by line. And store it in a sparse matrix .

I need the following function:

function mat = generate( path )
    fid = fopen(path);
    tline = fgetl(fid);
    % initialize an empty sparse matrix. (I know I assigned Mat(1, 1) = 1)
    mat = sparse(1);
    while ischar(tline)
        tline = fgetl(fid);
        if ischar(tline)
            C = strsplit(tline);
        end
        mat(C{1}, C{2}) = 1;
    end
    fclose(fid);
end

      

But unfortunately, other than the first line, it just puts trash in my sparse rug. Demo video:

1 7
1 9
2 4
2 9

      

If I print a sparse checkmate, I get:

   (1,1)        1
  (50,52)       1
  (49,57)       1
  (50,57)       1

      

Any suggestions?

+3


source to share


1 answer


Fixing what you have ...

Your problem is that C

- it is a cell array of characters , not numbers. You need to convert the strings you read from the file to integer values. Instead, strsplit

you can use functions like str2num

and str2double

. Since it tline

is a space-delimited character array in this case, it str2num

is the easiest to use to calculate C

:

C = str2num(tline);

      

Then you just index C

as an array instead of a cell array:

mat(C(1), C(2)) = 1;

      



Additional tidbit: If you're wondering how your demo code still worked even if it C

contains characters, that's because MATLAB tends to automatically convert variables to the correct type for certain operations. In this case, the characters were converted to their double ASCII code equivalents: '1'

became 49

, '2'

became 50

, etc. He then used them as indices in mat

.


The simplest alternative ...

You don't even need to worry about all this mess above, as you can replace your entire function in a much simpler way using dlmread

and sparse

:

data = dlmread(filePath);
mat = sparse(data(:, 1), data(:, 2), 1);
clear data;  % Save yourself some memory if you don't need it any more

      

+3


source







All Articles