Search code examples
arraysmatlabmedian

How to take the median of the following a 4D array (31x6x2x3)?


I am trying to get the median of each row of the same columns of the following 4D array:

df = randn(31,6,2,3)

What I basically have is 31 rows (i) of 6 variables (j) for two shocks (k), all this repeated 3 times (n). Now, focus on the first shock and get a 31x6x3 array:

eg1 = squeeze(df(:,:,1,:)) %31x6x3
  

What I want to get is the median of every row of the same column: e.g. the first row of the first column of the 3 repetitions, then the second row of the first column of the 3 repetitions, etc. Easier to see like this:

% median of every row of the first column in the 3 repetitions

median(eg1(1,1,:))
median(eg1(2,1,:))
median(eg1(3,1,:))

...

median(eg1(31,1,:))

% median of every row of the second column in the 3 repetitions

median(eg1(1,2,:))
median(eg1(2,2,:))
median(eg1(3,2,:))
...
median(eg1(31,2,:))

% basically the median of every row of the same column for every column
  

This shall be done for either shocks 1 and 2.

Can anyone help me with this?


Solution

  • What you want is the median of the last dimension. median accepts desired dimension as input.

    out=median(eg1,3)

    out will be a 31x6 matrix.

    If you want that from all of the egx, you just want the median of the last dimension

    out=median(df,4)