user1774893
user1774893

Reputation: 31

Matlab: Structure with 6x6 matrices - how to get the average across the group

I have a structure named 'data' with 100 entries, each corresponding to a participant from an experiment. Each of the 100 entries contains multiple 6x6 matrices giving different values.

For instance, an example of a matrix from my first participant is:

data.p001.matrixCB

   18.9737   17.0000   14.2829   12.4499   11.7898   10.0995
   18.1384   16.0000   13.4907   11.7898   11.2250   10.3441
   14.7986   12.5300   11.7898   11.7473   12.2066    9.3808
   14.3527   13.4536   12.9615   13.3417   12.7279   11.7047
   18.0278   17.8885   17.6068   17.4642   17.1464   16.6132
   24.1661   24.7790   23.7697   23.3880   22.6495   23.8537

...and this is one of 100 entries in the structure with a similar setup.

I'd like to get the mean average value for each cell in the matrix across my 100 participants. So I would have a mean value for the 100 values in position matrixCB(1,1), and all other positions in the matrix. Unfortunately I can't see how this is done, and the help functions are less than helpful. Any assistance would be greatly appreciated!

Upvotes: 0

Views: 1528

Answers (2)

paddy
paddy

Reputation: 63481

Structures can be a pain. To avoid typing out a bunch of code, you could take the following approach:

  1. Convert required matrices to cell array
  2. Reshape the cell array into 3D matrix
  3. Compute means across 3rd dimension

Code for this:

Mcell = arrayfun(@(x) data.(sprintf('p%03d',x)).matrixCB, 1:100, 'uni', 0);
M = mean( reshape(cell2mat(Mcell), 6, 6, []), 3 );

Upvotes: 1

URL87
URL87

Reputation: 11032

You can sum all your 100 matrix into Sum and then divide it by 100 - Sum./100 and then each cell would represent the avg of all 100 cells on each index .

For example -

Sum = A + B ; 
Sum./2 ; 

Upvotes: 1

Related Questions