1

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

2
  • Can you give an example with an object, e.g. a above? I get ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() when I try use this on an array object. Shape of the input array is 1D - [365,] Commented Oct 18, 2021 at 14:13
  • I'm new to python and clearly need to find time for training in it. If you'll write your broadcast solution as an answer, I can mark it as such. Thanks Commented Oct 18, 2021 at 16:08

2 Answers 2

2

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 ith value of the initial array (a[i]), by the way.

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

Comments

-1

I'm adding a broadcast_to() answer that a user bizarrely responded with then deleted, because I did ask whether it was possible in the original question. Thanks to both @dsillman2000 and the phantom user.

np.broadcast_to(a.reshape(-1,1,1), (len(a),360,720))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.