Reputation: 188
I want to do essentially what this person is doing but in 3D:
How can I add small 2D array to larger array?
Currently I am generated a large empty numpy array:
BigArray = np.zeros((2048,2048,1000), np.float16)
Then I am trying to add data into this array using the method shown in the stackoverflow question I linked.
The array I want to add is a smaller numpy array of 100,100,100 dimensions and the locations are given in a list of X,Y,Z coords that define the centre. Therefore my code reads as follows:
SmallArray = np.random.random((100,100,100))
X = 1000
Y = 1000
Z = 500
BigArray([X-49:Y+50, Y-49:Y+50, Z-49:Z+50]) = SmallArray
However I am getting an error that the first colon is a syntax error and I am not sure why.
Any and all help is much appreciated, thank you.
edit: typos.
Upvotes: 0
Views: 184
Reputation: 2132
It is interesting for me to write an very simple similar program:
#!/usr/bin/env python3
# encoding: utf-8
#如果上述二列對調,卻使用./執行的話程式就會進入無窮迴圈
#此列就是註解
#template定義時間:2019/7/5 v0.2
import sys
print (f'Python版本號={sys.version}')
#---開始include---
import numpy as np
#---開始定義class---
#---開始定義function---
print ('sam說:---主程式開始---')
if __name__=='__main__':
big_arr1 = np.random.rand(5,5,5)
small_arr1 = np.zeros((3,3,3), np.float16)
center_x = center_y = center_z = 2
#center_x-1:center_x+2代表有center_x-1、center_x、center_x+1三個
# center_y-1:center_y+2代表有center_y-1、center_y、center_y+1三個
# center_z-1:center_z+2代表有center_z-1、center_z、center_z+1三個
big_arr1[center_x-1:center_x+2,center_y-1:center_y+2,center_z-1:center_z+2]=small_arr1
print(f'big_arr1={big_arr1}')
print ('sam說:---程式結束---')
And it shows the result:
C:\Users\sam_lin\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/sam_lin/PycharmProjects/untitled/t1.py
Python版本號=3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]
sam說:---主程式開始---
big_arr1=[[[0.97776624 0.67305297 0.46804029 0.49918114 0.5827188 ]
[0.13722586 0.74721955 0.4252602 0.0834001 0.46595479]
[0.81273576 0.93105509 0.3550111 0.86397551 0.43013513]
[0.9162833 0.73321085 0.30299702 0.88372877 0.08543336]
[0.28145705 0.21005119 0.66785858 0.46601126 0.08057115]]
[[0.96049458 0.92891106 0.59779765 0.24660834 0.28381255]
[0.78940735 0. 0. 0. 0.79491548]
[0.65879618 0. 0. 0. 0.22152894]
[0.56392902 0. 0. 0. 0.543028 ]
[0.14708571 0.16269789 0.37162561 0.19991273 0.65671269]]
[[0.78414023 0.77322729 0.93637718 0.5180622 0.18265001]
[0.68605786 0. 0. 0. 0.50407885]
[0.98517971 0. 0. 0. 0.04644972]
[0.93118566 0. 0. 0. 0.79937072]
[0.97311219 0.47284717 0.82679315 0.76999545 0.82846718]]
[[0.0707382 0.46587191 0.89547718 0.85520751 0.62759826]
[0.4973629 0. 0. 0. 0.97606185]
[0.99327387 0. 0. 0. 0.28557434]
[0.31332736 0. 0. 0. 0.34476194]
[0.12757201 0.90843887 0.68510409 0.98262188 0.48204498]]
[[0.93682369 0.88788666 0.17488394 0.68499998 0.09336082]
[0.39821201 0.39974341 0.14220433 0.55088245 0.09949949]
[0.1348445 0.11631945 0.9976837 0.21023914 0.73043136]
[0.91621389 0.4786832 0.66673538 0.37927733 0.92164015]
[0.9259155 0.91651767 0.98076657 0.83919033 0.95723923]]]
sam說:---程式結束---
Process finished with exit code 0
Upvotes: 0
Reputation: 1265
Just remove the parenthesis:
BigArray[X-49:Y+50, Y-49:Y+50, Z-49:Z+50] = SmallArray
Also you will need to fix the dimensions since as-is they do not match, i.e.:
BigArray[X-50:Y+50, Y-50:Y+50, Z-50:Z+50] = SmallArray
Edit:
Also you have couple of typos in the code (like using zeroes
instead of zeros
and np.random
instead of np.random.random
), but I assume that you are not interested in those
Upvotes: 2