View Single Post
  #5   Spotlight this post!  
Unread 03-03-2015, 18:38
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: can Python open a matrix saved by Octave in sparse format?

Here's a bit of a quick hack:

Code:
import scipy.sparse
import numpy

def read_sparse(filename):
  (r, c, d) = numpy.transpose(numpy.loadtxt(filename, 'int'))
  return scipy.sparse.coo_matrix((d, (r-1, c-1)))
Note that the matrix's dimensions may be truncated if there are trailing rows and columns that are all 0, but this is not the case with your example matrix. It would need a little more work to read the dimensions from the file.

---

A better approach would be to have Octave save the data into MATLAB format (using something like
save -6 A_sparse_octave.mat A), then read it in Python using scipy.io.loadmat
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
Reply With Quote