1

I have several N-dimensional arrays of different shapes and want to combine them into a new (N+1)-dimensional array, where the new axis has a length corresponding to the number of initial N-d arrays.

This answer is sufficient if the original arrays are all the same shape; however, it does not work if they have different shapes.

I don't really want to reshape the arrays to a congruent size and fill with empty elements due to the subsequent analysis I need to perform on the final array.

Specifically, I have four 4D arrays. One of the things I want to do with the resulting 5D array is plot parts of the four arrays on the same matplotlib figure. Obviously I could plot each one separately, however soon I will have more than four 4D arrays and am looking for a dynamic solution.

2
  • You cannot put arrays of different shape in one array without filling the gaps with 0 or other numbers. Commented Jun 9, 2016 at 16:58
  • 1
    Just put the arrays in a Python list. Then you can iterate over them to plot them. Commented Jun 9, 2016 at 16:59

1 Answer 1

3

While I was writing this, Sven gave the same answer in the comments...

Put the arrays in a python list in the following manner:

5d_list = []
5d_list.append(4D_array_1)
5d_list.append(4D_array_2)
...

Then you can unpack them:

for 4d_array in 5d_list:
    #plot 4d array on figure
Sign up to request clarification or add additional context in comments.

Comments

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.