-2

How can I extract a submatrix from matrix in python?

I have a .mat file that when uploaded to python has a nested array that is something similar to the following but with 12,000 items each:

letters= array([[(array([[a],[a],[a]]), array([[b],[b],[b]]),array([[c],[c],[c]])]])

I want to separate each submatrix into a matrix, that will be something like this:

letter_a= array([[a],[a],[a]])
letter_b= array([[b],[b],[b]])
letter_c= array([[c],[c],[c]])

I want this separation because I want to work with each individual array

type(letter)

Thanks!

NEURO
  • 1
  • 1
  • What kind of arrays are they? Numpy? – internet_user Jan 24 '18 at 14:38
  • 2
    Are you sure this is an array of arrays? – jpp Jan 24 '18 at 14:39
  • can you see what `type(letters)` returns? – usernamenotfound Jan 24 '18 at 14:42
  • Assuming your `array` is just `np.array` go with normal indexing when getting things out `letters` with `letters[0][0]` to get first row, `letters[0][1]` to get 2nd one and `letters[0][2]` to get the last one. However you may want to rethink the way of writing this "array of arrays", because it's just a big mess. – Sqoshu Jan 24 '18 at 14:44

1 Answers1

0

Function array() dose not existed in python. You may use nested list instead. Hope this is helpful.

letters = [[['a'],['a'],['a']], [['b'],['b'],['b']], [['c'],['c'],['c']]]
letter_a = letters[0] # --> [['a'],['a'],['a']]
letter_b = letters[1] # --> [['b'],['b'],['b']]
letter_c = letters[2] # --> [['c'],['c'],['c']]

Update

As some friend kindly pointed out, array indeed existed in standard python library. I apologize for my careless thinking and rush answer at the beginning. Thank you for your kind correction. Since the array function in standard module is for numbers, I guess it is not what is meant here. But really thanks for any helpful correction. At the end, I just hope I could help the person with questions and it turns out I also learn a lot from other people. This is the part of this forum that I really enjoy and appreciate.

englealuze
  • 1,445
  • 12
  • 19