how to make each element in an array another array
Question:
Hi I came across a problem. I have a numpy array with size (256, 144). Each element is 0 in this array. Now I want to make each element in the array to be [0, 0, 0]. Is there a way of doing this?
The code is:
empty_windows = np.zeros(256, 144)
for i in range(256*144):
empty_windows[i] = [0,0,0]
This method doesnt work as it returns an error message "ValueError: setting an array element with a sequence."
Is there a way of doing this? Thank you very much.
Answers:
If you don’t need to do anything with empty_windows
while it has size (256, 144), you can simply create it with the proper size:
empty_windows = np.zeros((256, 144, 3))
You could try creating a second numpy array with the required shape, which in your case is (256,144,3)
, iterating over it and correcting the elements as required.
empty_windows = np.zeros((256, 144))
x = np.random.rand(256,144,3)
for i in range(256):
for j in range(144):
x[i][j] = [empty_windows[i][j] for _ in range(3)]
empty_windows = x
You may also want to brush up on your understanding of numpy arrays, as the commenter mentioned, particularly how to initialize and iterate over them.
Hi I came across a problem. I have a numpy array with size (256, 144). Each element is 0 in this array. Now I want to make each element in the array to be [0, 0, 0]. Is there a way of doing this?
The code is:
empty_windows = np.zeros(256, 144)
for i in range(256*144):
empty_windows[i] = [0,0,0]
This method doesnt work as it returns an error message "ValueError: setting an array element with a sequence."
Is there a way of doing this? Thank you very much.
If you don’t need to do anything with empty_windows
while it has size (256, 144), you can simply create it with the proper size:
empty_windows = np.zeros((256, 144, 3))
You could try creating a second numpy array with the required shape, which in your case is (256,144,3)
, iterating over it and correcting the elements as required.
empty_windows = np.zeros((256, 144))
x = np.random.rand(256,144,3)
for i in range(256):
for j in range(144):
x[i][j] = [empty_windows[i][j] for _ in range(3)]
empty_windows = x
You may also want to brush up on your understanding of numpy arrays, as the commenter mentioned, particularly how to initialize and iterate over them.