Ask Your Question

Revision history [back]

I had a look on the web, and it seems that you are speaking about .mat matrices provided by matlab. So, scipy, which is shipped with Sage already has a tool to read .mat files:

sage: from scipy.io import loadmat
sage: M = loadmat('<path_to_your_file.mat>')

As you can see, the object M is a dictionary:

sage: M

What is of interest for us is the a field:

sage: M['a']
array([[[  1.,   4.,   7.,  10.],
        [  2.,   5.,   8.,  11.],
        [  3.,   6.,   9.,  12.]]])

Which is an aray with a single entry that is the array we want:

sage: M['a'][0]
array([[  1.,   4.,   7.,  10.],
       [  2.,   5.,   8.,  11.],
       [  3.,   6.,   9.,  12.]])

This can easily be transformed into a Sage matrix:

sage: m = matrix(RDF,M['a'][0])
sage: m
[ 1.0  4.0  7.0 10.0]
[ 2.0  5.0  8.0 11.0]
[ 3.0  6.0  9.0 12.0]
sage: m.parent()
Full MatrixSpace of 3 by 4 dense matrices over Real Double Field

I had a look on the web, and it seems that you are speaking about .mat matrices provided by matlab. So, scipy, which is shipped with Sage already has a tool to read .mat files:

sage: from scipy.io import loadmat
sage: M = loadmat('<path_to_your_file.mat>')

As you can see, the object M is a dictionary:

sage: M

What is of interest for us is the a field:

sage: M['a']
array([[[  1.,   4.,   7.,  10.],
        [  2.,   5.,   8.,  11.],
        [  3.,   6.,   9.,  12.]]])

Which is an aray with a single entry that is the array we want:

sage: M['a'][0]
array([[  1.,   4.,   7.,  10.],
       [  2.,   5.,   8.,  11.],
       [  3.,   6.,   9.,  12.]])

This can easily then be transformed into a Sage matrix:

sage: m = matrix(RDF,M['a'][0])
sage: m
[ 1.0  4.0  7.0 10.0]
[ 2.0  5.0  8.0 11.0]
[ 3.0  6.0  9.0 12.0]
sage: m.parent()
Full MatrixSpace of 3 by 4 dense matrices over Real Double Field

I had a look on the web, and it seems that you are speaking about .mat matrices provided by matlab. So, scipy, which is shipped with Sage already has a tool to read .mat files:

sage: from scipy.io import loadmat
sage: M = loadmat('<path_to_your_file.mat>')

As you can see, the object M is a dictionary:

sage: M

What is of interest for us is the a field:

sage: M['a']
array([[[  1.,   4.,   7.,  10.],
        [  2.,   5.,   8.,  11.],
        [  3.,   6.,   9.,  12.]]])

Which is an aray with a single entry that is the array we want:

sage: M['a'][0]
array([[  1.,   4.,   7.,  10.],
       [  2.,   5.,   8.,  11.],
       [  3.,   6.,   9.,  12.]])

This can then be transformed into a Sage matrix:

sage: m = matrix(RDF,M['a'][0])
sage: m
[ 1.0  4.0  7.0 10.0]
[ 2.0  5.0  8.0 11.0]
[ 3.0  6.0  9.0 12.0]
sage: m.parent()
Full MatrixSpace of 3 by 4 dense matrices over Real Double Field

If your matrix is the adjacency matrix of a graph, it should be square and symmetric, in which case you can recover the graph with:

sage: G = Graph(m)