convert edge list to adjacency matrix
1 view (last 30 days)
Show older comments
How to convert an edge list with 5000000 nodes to adjacency matrix in Matlab. I tried the following code:
fid = fopen('C:\myfile.txt', 'r');
E = fscanf(fid, '%g %g', [2 inf]);
E=E';
adj=sparse(E(:,1),E(:,2),1);
adj=full(adj);
fclose(fid);
I am getting memory error. Is there any way out?
Answers (1)
Walter Roberson
on 27 Mar 2017
A non-sparse adjacency matrix with 5 x 10^6 members in it would require a minimum of (5 * 10^6)^2 = 25 * 10^12 bytes, which would be about 22 3/4 petabytes. If you have that much memory available, then you should change your code from
adj=sparse(E(:,1),E(:,2),1);
to
adj = sparse(E(:,1), E(:,2), true);
to create a sparse logical array; then when you do the full() it will be a logical array that is created instead of a double array.
Note: many of the common algorithms you would want to apply to such as matrix will likely require that the data be converted to floating point, and will probably need temporary storage as large as that floating point array, so if you run out of memory using sparse() the way you are calling it now, chances are good that using a logical array instead of a double array will not reduce your memory problems enough to be able to do useful work.
0 Comments
See Also
Categories
Find more on Logical 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!