0

Suppose there we have an array of shape(10, 20, 30).

What I would like to get are all the:

  1. 20*30 arrays of shape(10),
  2. 10*30 arrays of shape(20),
  3. 10*20 arrays of shape(30).

Locking all dimensions but one manually is easy, but I am struggling to write an efficient loop to iterate this way. The reason for this is that in my application case, I will have several dimensions which will vary between 2 to 5, and I will need to get all subarrays of locking all dimensions but one.

I thought I could use transpose and roll to create an index which rolls, such as

data = np.zeros((10, 20, 30))
for ndim in range(len(data.shape)):
   index = np.arange(0, len(data.shape)), 1)
   index = np.roll(index, 1)
   _data = np.transpose(data, index)[0]

... and then I realized this is useless, because its the opposite of what I need.

I got to the point I even tried asking chatgpt. So it's time to ask for help. I am utterly stuck

1 Answers1

0

I'm not totally sure exactly what you're asking for, but perhaps it's something like this?

for i in range(data.ndim):
    data2 = np.moveaxis(data, i, 0)
    for index in np.ndindex(data2.shape[1:]):
        print(data2[..., index])

This code moves each axis in data to the first axis, and then loops over the remaining two dimensions.

Roy Smart
  • 664
  • 4
  • 12