2

I want to create a matrix that contains matrix elements. So I did the obvious thing and made this:

import numpy as np

A = np.array([1,2,3,1],[3,1,5,1])
B = np.array([1,6,8,9],[9,2,7,1])
E = np.array([A, B],[B, A])

But the compiler returns: TypeError: data type not understood

What can I do to create such a matrix, because I have really huge matrices and I dont have the time to explicitly write everyone down ?


* Edit 1: *

Additional problem that occurred:

enter image description here

Instead of getting a 14x14 matrix, I am getting a multidimensional (2,2,7,7) matrix. Where in the simplified version that was my original question , everything is sound. Any ideas why this occurs now?

In this case I have the Amat 7x7, Bmat 7x7 , Emat 14x14, Smat 14x14


Edit 2

Ok I solved the problem using the np.block() as it was stated in the comments below. Thank you very much.


3
  • 1
    You got an error on the A= line, didn't you? It didn't even get to the E step. In other words, it's a basic problem with using np.array. Commented Sep 7, 2018 at 16:11
  • 2
    Correcting for the np.array syntax, there's still some ambiguity as to what you want. One answer produces a (4,8) array, another (2,2,2,4). Commented Sep 7, 2018 at 16:14
  • 3
    One nitpick: you almost certainly want a NumPy array (type ndarray) rather than a matrix. The distinction is important: NumPy does have a matrix type, but it can be awkward to use and isn't always well-integrated with the rest of NumPy or with other NumPy-using libraries. With the @ operator for matrix-style multiplication available in recent times, there aren't very many good reasons for using the matrix type. Commented Sep 7, 2018 at 16:17

3 Answers 3

4

Assuming that you want a two-dimensional array of shape (4, 8) as a result, it sounds as though you're looking for numpy.block. It's available since NumPy 1.13, and as the name suggests, it creates a new array out of blocks, where each block is an existing array.

You also need an extra pair of square brackets in the calls that create A and B. The signature of numpy.array is:

array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)

So if you write np.array([1, 2, 3, 1], [3, 1, 5, 1]) then you're passing two arguments to the array function, and the second argument will be interpreted as the dtype: i.e., the desired datatype of the elements of the array. This is why you're getting the "data type not understood" error. Instead, you want to pass a nested list-of-lists as the first argument: np.array([[1, 2, 3, 1], [3, 1, 5, 1]]).

Putting it all together:

>>> import numpy as np
>>> A = np.array([[1, 2, 3, 1], [3, 1, 5, 1]])
>>> B = np.array([[1, 6, 8, 9], [9, 2, 7, 1]])
>>> E = np.block([[A, B], [B, A]])
>>> A
array([[1, 2, 3, 1],
       [3, 1, 5, 1]])
>>> B
array([[1, 6, 8, 9],
       [9, 2, 7, 1]])
>>> E
array([[1, 2, 3, 1, 1, 6, 8, 9],
       [3, 1, 5, 1, 9, 2, 7, 1],
       [1, 6, 8, 9, 1, 2, 3, 1],
       [9, 2, 7, 1, 3, 1, 5, 1]])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. But when I run the program the Variable Explorer ( I am using Spyder) says that the Size of my original matrix E is (2,2,7,7) instead of the (14,14). What is happening?
@GeometricalFlows: Read the part about numpy.block again.
3

and welcome to Stack Overflow!

The np.array wants the first argument to be the matrix and the second argument to be the datatype.

Here, you're actually sending in two lists as the first two arguments. Since a list of numbers is not a datatype, it does not understand what you're trying to do. You need to enclose it in a list:

import numpy as np

A = np.array([[1,2,3,1],[3,1,5,1]])
B = np.array([[1,6,8,9],[9,2,7,1]])
E = np.array([[A, B],[B, A]])

To send in a datatype, you could do e.g.

D = np.array([1,2,3,4,5], np.float32). Now the second argument is an actual datatype, and not a list.

Comments

1

You were close, just missing a few brackets. For a matrix you need to give np.array a list of lists.

In [4]: import numpy as np
   ...: 
   ...: A = np.array([[1,2,3,1],[3,1,5,1]])
   ...: B = np.array([[1,6,8,9],[9,2,7,1]])
   ...: E = np.array([[A, B],[B, A]], dtype=int)
   ...: 

In [5]: E
Out[5]: 
array([[[[1, 2, 3, 1],
     [3, 1, 5, 1]],

    [[1, 6, 8, 9],
     [9, 2, 7, 1]]],


   [[[1, 6, 8, 9],
     [9, 2, 7, 1]],

    [[1, 2, 3, 1],
     [3, 1, 5, 1]]]])

2 Comments

These are braces: {}. They are sometimes called curly brackets. These are brackets: [], sometimes called square brackets when there could be confusion otherwise.
Awesome that you're making answers! I hope you wouldn't mind just a few pointers? When someone has an error, especially if OP is new on the site, it's often reasonable to assume that the OP does not know what this error means. If you provide links and a description of why the error happens, you can expect to get a lot more rep on your answers! Again: it's great that you're trying to help, and that's only what I'm trying to do too!

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.