# How to input from file?

I have a file with set of matrices in a file. How to put them into array of matrices in sage? Example of file:

[1,0,0]

[0,1,0]

[1,1,1]

[0,1,0]

[1,0,0]

[0,0,1]

How to input from file?

I have a file with set of matrices in a file. How to put them into array of matrices in sage? Example of file:

[1,0,0]

[0,1,0]

[1,1,1]

[0,1,0]

[1,0,0]

[0,0,1]

add a comment

1

For small number of small-sized matrices like you list above, you could do the following.

Create a file named `mat.sage`

that contains:

```
a=matrix([[1,0,0],
[0,1,0],
[1,1,1]])
b=matrix([[0,1,0],
[1,0,0],
[0,0,1]])
```

Then, in your Sage worksheet, use the command `load('mat.sage')`

to read the matrices into memory. You can then begin computing with these matrices. For example, your worksheet might include:

```
load('mat.sage')
a*b
```

which gives a result of

```
[0 1 0]
[1 0 0]
[1 1 1]
```

For a very large number of matrices, there are a number of ways you might approach this.

1) You could treat it like one huge matrix and read just that one matrix in. Then, you could break it out into smaller matrices. I don't know if this will result in a memory issue or not.

2) You could read the data in from a csv file and then convert the appropriate parts to matrices. (This is what I would do.)

Asked: **
2015-03-17 11:17:22 -0600
**

Seen: **1,388 times**

Last updated: **Mar 18 '15**

Map a function to each element of a matrix?

Given a value and conditions, find a matrix with that value as its determinant

Where is the sage save or load dir and how do I change it?

Get a matrix to display answers as decimals/floats, not fraction?

Smith-McMillan Form of a polynomial matrix

Can I use the block_matrix command to create arbitrary sizes of block matrix arrays?

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.