1

I am trying to stack a few 2-D arrays to obtain a 3-D array in python. My motivation behind doing this is to plot the spatial dependence of a variable: var(x,y) where x and y are two different dimensions, for different parameter values param. A minimum reproducible example for the same has been attached below:

while (param<=2):
 var(x,y)= fun  # fun is a function which returns var(x,y) as mentioned above
 param+=1

I want to keep on stacking the 2-D arrays var(x,y) for different values of param, along a 3rd dimension. My output should be of the form :- For param=0 : var(x,y), For param=1 : var(x,y), For param=2 : var(x,y). Any help regarding the construction of the 3-D array would be highly appreciated. Please note that var(x,y) is a 2-D array.

3
  • Usually we recommend collecting the arrays in a list, and doing one array construction step at the end. Whether you use np.array, np.stack or one of the other variations on np.concatenate depends on the desired shape(s). Commented Jun 2, 2020 at 6:21
  • But for this problem, I already have the 2d arrays ready. Could you suggest me how to reconstruct a 3d array from this. Commented Jun 2, 2020 at 6:28
  • Because I don't want to complicate things more. The reason for already having 2-d arrays built is that it gives a better physical essence to the problem in terms of the spatial dependence of the variable. Commented Jun 2, 2020 at 6:33

1 Answer 1

2

As suggested by hpaulj, the key idea is to have a list of the 2D arrays and then assemble them into a 3D array. It's usually more efficient to create the array in one go (no appending), so the following should be ideal:

import numpy as np

var_lst = []  # This is where you store the var arrays
while (param<=2):
    var = fun  # fun is a function which returns var(x,y) as mentioned above
    var_lst.append(var)
    param+=1

# Creates a 3D array with the following axes (x, y, param)
var_arr = np.stack(var_lst, 2)

# If later you want different axes, like (param, x, y), you can always swap them around with transpose
var_arr = var_arr.transpose(2, 0, 1)
Sign up to request clarification or add additional context in comments.

4 Comments

With np.stack you can chose the axis and skip the swap.
Thanks for your answer. One question :- keeping up with generality, if I am sweeping param *** n*** number of times, then should I use var_arr=np.stack(var_list,(n-1)), one more question:- how do I access the elements of the 3-D array along the 3rd dimension. Will normal indexing technique work?
1st question: no, the 2 in np.stack just indicated the axis on which the arrays within the list are to be stacked. For example, if var(x,y) is a 2x2 array, and if param=n, by doing var_arr = np.stack(var_lst, 2) the shape of var_arr will be 2x2xn. If you were to do var_arr = np.stack(var_lst, 0), the shape of var_arr would now be nx2x2, because you are stacking on the first axis. 2nd question: the normal indexing works, you if you want var(x, y) for param=3 you just do var_arr[:, :, 3]
My question was not for ``` param=n``` . I meant to ask : if i am sweeping the parameter a few times (let's say n number of times). The what should I do?

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.