0

Hello I have the following question. I create zero arrays of dimension (40,30,80). Now I need 7*7*7 of these zero arrays in an array. How can I do this? One of my matrices is created like this:

import numpy as np
zeroMatrix = np.zeros((40,30,80))

My first method was to put the zero matrices in a 7*7*7 list. But i want to have it all in a numpy array. I know that there is a way with structured arrays I think, but i dont know how. If i copy my 7*7*7 list with np.copy() it creates a numpy array with the given shape, but there must be a way to do this instantly, isnt there?

EDIT

Maybe I have to make my question clearer. I have a 7*7 list of my zero matrices. In a for loop all of that arrays will be modified. In another step, this tempory list is appended to an empty list which will have a length of 7 in the end ( So i append the 7*7 list 7 times to the empty list. In the end I have a 7*7*7 List of those matrices. But I think this will be better If I have a numpy array of these zero matrices from the beginning.

4
  • 1
    Please clarify this sentence "Now I need 7*7 of these zero arrays in a array." Commented Mar 1, 2018 at 13:24
  • 3
    np.zeros((7, 7, 40, 30, 80))? Commented Mar 1, 2018 at 13:29
  • 1
    np.frompyfunc(zeroMatrix.copy, 0, 1)(np.empty((7, 7, 7), object)) gives an array of arrays. Commented Mar 1, 2018 at 15:16
  • exactly what i searched for! Thank you:) I would upvote it, but maybe you write it as an answer :) Commented Mar 1, 2018 at 16:41

1 Answer 1

2

Building an array of same-shaped arrays is not well supported by numpy which prefers to create a maximum depth array of minimum depth elements instead.

It turns out that numpy.frompyfunc is quite useful in circumventing this tendency where it is unwanted.

In your specific case one could do:

result = np.frompyfunc(zeroMatrix.copy, 0, 1)(np.empty((7, 7, 7), object))

Indeed:

>>> result.shape
(7, 7, 7)
>>> result.dtype
dtype('O')
>>> result[0, 0, 0].shape
(40, 30, 80)
Sign up to request clarification or add additional context in comments.

Comments

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.