0

Is it possible to fulfill numpy arrays with arrays? I want to obtain a following structure without specifying values by hand

ves = np.zeros((12,12), dtype=object)
ves[0][0] = np.array([0,0,0])
ves[0][1] = np.array([0,0,0])
ves[0][2] = np.array([0,0,0])
ves[0][3] = np.array([0,0,0])
and so on...

In order to obtain the expected result, I have tried ves = np.zeros((12,12), dtype=array), but it does not work.

4
  • What happens when you try it? Commented Mar 14, 2017 at 13:05
  • It works fine, but I do not want to put values np.array([0,0,0]) by hand. I have tried specified dtype=array, but it didn't work Commented Mar 14, 2017 at 13:07
  • It seems like you want a 3D tensor. Can you try ves = np.zeros((12, 12, 3), dtype="int32")? Commented Mar 14, 2017 at 13:16
  • Do you understand the difference between this (12,12) object array and a (12,12,3) integer array? Commented Mar 14, 2017 at 13:20

1 Answer 1

2
import numpy as np 

v = np.zeros([12,12,3])

As per my understanding through your explanation, it seems you wanted a three dimension matrix where each cell needs three 0 values for 12*12 places. So the above code creates the value filled ndarray.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Could you help me to understand what is the difference between np.zeros([12,12,3]) and np.zeros((12,12,3))?
There is no difference in the result. In the first case you input a list (marked by square brackets) in the second you input a tuple (round brackets). See here for more details: stackoverflow.com/questions/626759/…

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.