I have a 1 dimensional array of CO2 values where I need to repeat each value over lon/lat dimensions [360, 720] creating an array of dims [365, 360, 720] - i.e. 1 year of daily CO2 values across longitude and latitude.
I would want to take an array:
a = np.array([343.79258065, 343.79096774, 343.78935484])
And tile the first value across an array of dims [360, 720], then tile the second value across the same dims [360, 720], and do that for n values in the array (365 times in my case)
A small dim example of this would be (but please note the dims I want below):
array([[343.79258065, 343.79258065, 343.79258065, ...],
[343.79096774, 343.79096774, 343.79096774, ...],
[343.78935484, 343.78935484, 343.78935484, ...]])
The output dimensions
So effectively each value in array a
would be repeated (tiled?) over an array of dims [360, 720] for 365 layers resulting in a 3D array of dims [365, 360, 720].
It would be great to solve this with broadcast_to()
if it's possible. Some links that don't quite do what I want: Repeating values n times and Repeat across multiple dimensions
CodePudding user response:
I hope I understand your question correctly! I would do this by first reshaping the array into (N,1,1)
, then expanding each of those 1x1 sub-arrays into the repeated arrays you wanted to be (360, 720) using numpy's np.repeat function. Here's how I did it:
a = np.array([343.79258065, 343.79096774, 343.78935484]).reshape(-1,1,1)
a = np.repeat(a, 360, axis=1)
a = np.repeat(a, 720, axis=2)
print(a.shape)
>> (3, 360, 720)
If this isn't what you had in mind, please let me know! Each of the N
360x720 arrays contains only repeated instances of the i
th value of the initial array (a[i]
), by the way.