1

I'm looking for a way to import N matrices from one file to a single 3D array. I know the number of rows m and the number of columns n of each matrix (respectively listed in two lists m[N] and n[N]).

For example I have N=3 matrices like these:

1  2   3   4
5  6   7   8
9  10  11  12
1   2   3
4   5   6
7   8   9
10  11  12
1  2  3  4   5   6 
7  8  9  10  11  12

They are matrices of dimension 3x4, 4x3, 2x6 respectively, so in this case m = [3,4,2] and n = [4,3,6]. I would store them in an object of the type M[i][j][k] where i,j,k are the indeces that identify the matrix, the row and the column respectively (in this case M[1][2][0]=7).

For example in C++ this can be easily done:

ifstream file ("filename.txt");
  for(int i = 0; i < N; i++)
    for(int j = 0; j < m[i]; j++)
      for(int k = 0; k < n[i]; k++)
        file >> M[i][j][k];

file.close();

Thanks in advance.

9
  • have you checked numpy or nested python list? Commented Feb 4, 2021 at 12:53
  • can you show M? Commented Feb 4, 2021 at 13:03
  • @Epsi95, I usually use numpy.genfromtxt but here the problem is the different number of columns. Nested python list could be the right way, but i don't know how to import the data. Commented Feb 4, 2021 at 13:15
  • @python_user for the above example M[1][2][0] would give 7 Commented Feb 4, 2021 at 13:20
  • "I know the number of rows m and the number of columns n of each matrix." how do you have these m and n? Commented Feb 4, 2021 at 13:27

1 Answer 1

1

You can use a list comprehension with itertools.islice to get what you want. islice allows you to read lines from the file without having to read the whole file into the memory.

from itertools import islice

m = [3, 4, 2]
n = [4, 3, 6]

M = []

with open('matrix_data.txt') as file:
    for row_count in m:
        rows = islice(file, row_count) # read `row_count` lines from the file
        rows = [row.split() for row in rows] # split every line in the file on whitespace
        rows = [[int(cell) for cell in row] for row in rows] # convert to int
        M.append(rows) # add to `M`

for matrix in M:
    print(matrix)

Output

[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much!! So the n[N] is not needed.
not for the way I suggested, m is enough, you can upvote the answer as well :) if it helped
how can I convert M to a np.ndarray? thank you
you mean a 3d numpy array?
Exactly! Because in this way, for example the first row of the first matrix [1, 2, 3, 4] is a list.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.