user3654549
user3654549

Reputation: 99

How can I reshape this 2D array to a 4D array?

I have a 2D array of laminar flame speeds in the form of:

T1 P1 phi1 EGR1 S
T1 P2 phi1 EGR1 S
.
.
.
T1 P12 phi1 EGR1 S
T1 P13 phi1 EGR1 S
T1 P1 phi2 EGR1 S
T1 P2 phi2 EGR1 S
.
.
.
T1 P12 phi2 EGR1 S
T1 P13 phi2 EGR1 S
.
.
.
.
.
.
T28 P1 phi5 EGR1 S
T28 P2 phi5 EGR1 S
.
.
.
T28 P12 phi5 EGR1 S
T28 P13 phi5 EGR1 S

Where there are 28 different initial temperature (T1 - T28), 13 different initial pressures (P1 - P13), and 5 different fuel-air ratios (phi1 - phi5). EGR level doesn't change (EGR1), and then the resulting flame speed is listed as S.

So this is a 2D array of size (1820,5). I would like to convert it to a 4D array in MATLAB in the form of: A(5,5,13,28). So each page is 5 fuel-air ratios at a specific pressure and temperature, i.e., 5x5. The next page (3rd dimension), changes the pressure to P2, but keeps the temperature the same. Then finally, the 4th dimension increases the initial temperature.

I am familiar with the reshape and permute functions, but just can't seem to get it to what I want. Can anyone lend a hand?

Upvotes: 1

Views: 810

Answers (1)

gnovice
gnovice

Reputation: 125854

This should give you the result you want, assuming data2D is in the form you give in your example:

data4D = permute(reshape(data2D, [13 5 28 5]), [2 4 1 3]);

First, note that in the 2D data the second column (P) cycles most rapidly, followed by the third column (phi) then the first column (T). This is why I chose [13 5 28 5] in the reshape step, which results in a 4D matrix where P varies across the first dimension, phi across the second, T across the third, and your row data across the fourth.

The permute step then simply reorganizes the dimensions in the order [2 4 1 3], such that phi varies across the first dimension, your row data across the second, P across the third, and T across the fourth.

Upvotes: 2

Related Questions