Mary
Mary

Reputation: 788

Calculating Average in Matrix

I have a simulation in Matlab that produces a 3D matrix (date, start point, trial) of size <103x11x6 double>. It is a simulation that returns a simulated interest rate over 103 time steps for 11 different starting values. This is repeated 6 times. I wish to simply create a new matrix that returns the average of all these trials. i.e. a single time series for each starting point where the time series is the average interest rate of all the trials for each time point. Many thanks for any help.

Upvotes: 0

Views: 10988

Answers (1)

mwengler
mwengler

Reputation: 2778

a 3D matrix (date, start point, trial)

If the matrix is M, then

A = mean(mean(M,3),2);

then A has one dimension date and each value of A is the average over the 11x6 matrix at that date. mean(M,3) is a 2-d matrix, which you can then take the mean of.

Interestingly,

A = mean(M(:,:),2);

will give you the same result. M(:,:) will collapse M down to 2 dimensions, with the first one preserved and all the later dimensions (only 2nd and 3rd in your case) collapsed to a single dimension.

Upvotes: 1

Related Questions