DiegoDZ
DiegoDZ

Reputation: 209

Create a matrix made of matrices with different sizes

I want to create a matrix which elements are matrices (with different sizes), vectors and numbers. For example, I have the next two matrices, one vector and one number:

A = [1 2 3
     4 5 6
     7 8 9]

B = [10 11
     12 13]

C = [14
     15
     16]

D = 17

And I would like to obtain a matrix 2x2, K, whith elements:
k_11 = A, k_12 = B, k_21 = C, k_22 = D.

The idea is to have the possibility to choose matrices, vectors or numbers of the big matrix, K, like they would be "simple" elements of a matrix. I.e.: K[0,0] = A, K[0,1] = B and so on.

Firstly, I thougth I could to obtain a list of matrices, vectors and numbers with K.append(A)..., but then I figured out that I will not be able to transform the list into a matrix.
And secondly, I tried to create a block matrix with numpy.bmat. The problem with bmat is that the dimensions of the inputs elements must match exactly.
Any idea?

Thanks.

Upvotes: 1

Views: 1460

Answers (2)

DiegoDZ
DiegoDZ

Reputation: 209

After reading the answers I created a 2D list but I did not use numpy.matrix because with the list I can choose the element of the "matrix" I want. Here the answer:

>>> K = [ ]
>>> K.append([ ])
>>> K.append([ ]) 
>>> K[0].append[A]
>>> K[0].append[C]
>>> K[1].append[B]
>>> K[1].append[D]

So, if I want to select the element A:

>>> K[0][0] 

And the element B:

>>> K[1][0]

Upvotes: 1

polwel
polwel

Reputation: 647

First, store the 4 objects in a 2D list, then make the list into a numpy.matrix.

K = matrix([[A, B], [C, D])

Upvotes: 3

Related Questions