help-octave
[Top][All Lists]

## Re: Reshaping a 4D array to 2D

 From: Nicholas Jankowski Subject: Re: Reshaping a 4D array to 2D Date: Thu, 12 Mar 2020 16:17:02 -0400

On Thu, Mar 12, 2020 at 3:47 PM Brett Green <address@hidden> wrote:
Hello all,

I need to reshape a 4D array H indexed as H(x,y,n,data) to a 2D array D indexed as D(u,data) where u consolidates the indices x, y and n. I can do this with a simple demonstration matrix as follows:

for x=1:2
for y=1:3
for n=1:4
for data=""> H(x,y,n,data) = x*1000+y*100+n*10+data;
end
end
end
end
D = reshape(H,24,5)

It is critical that the last index, data, is not rearranged in any way - H(x,y,n,1) and H(x,y,n,2) must be reshaped to D(u,1) and D(u,2) so that (1) they are indexed by the same number for data, and (2) there is a consistent mapping x,y,n -> u, so that u depends only on x,y,n and not on data.

reshape appears to satisfy both of these - the columns of D share the same value for data, and each row of D has the same values for x,y,n for all entries. The documentation simply states that column-major order is used, which if I understand correctly preserves the last index (in the case of a matrix the column, in this case data), as I wanted.

I have never worked with reshaping arrays before, so I will continue to try to understand why this works. In the meantime, would someone who understands better be able to confirm for me that reshape does indeed satisfy both the requirements I mentioned above?

Just to confirm - the script for the small array you wrote above provides the mapping that you're looking for? If so are you concerned that something about the reshape might not scale to larger arrays? If not, what do you actually want it to look like? can you provide a desired output example?