have a func(:,:,t) that I took the average of, how do I create just an array of those values?

1 view (last 30 days)
So I have iterations of a 2-D temperature profile, in which I do
Tavg = mean(mean(T(:,:,:)));
where T(i,j,t) is what I used.
Anyways, "Tavg" spits back a matrix of size [1 1 "#timesteps"]
Not sure why it's that size because when I type "Tavg" it gives me:
Tavg(:,:,17280) =
306.7141
an actual value. (note, that's one of thousands necessary for my script)
How would I take all those computed averages to be able to plot them against time? Seems trivial, but I can't figure it out.

Accepted Answer

Cedric
Cedric on 19 Mar 2013
Edited: Cedric on 19 Mar 2013
MEAN computes a mean along the 1st non-singleton dimension of its (1st) arg, unless you provide the dim as a second arg.
>> A = rand(2)
A =
0.4218 0.7922
0.9157 0.9595
>> mean(A)
ans =
0.6687 0.8758
that was along dim 1. Now if A had been a 1x2 array (dim 1 is a singleton):
>> A = rand(1,2)
A =
0.6557 0.0357
>> mean(A)
ans =
0.3457
you see that the mean was performed over dim 2 (as dim 1 is a singleton dimension).
In your case, the inner MEAN performs a mean along dim 1 and reduces it to singleton. The outter MEAN performs a mean along dim 2 (1st non-singleton). You are left with a 3D array with two singleton dims, which is what is displayed..
>> A = rand(2, 2, 3)
A(:,:,1) =
0.8491 0.6787
0.9340 0.7577
A(:,:,2) =
0.7431 0.6555
0.3922 0.1712
A(:,:,3) =
0.7060 0.2769
0.0318 0.0462
>> mean(A) % Along dim 1.
ans(:,:,1) =
0.8916 0.7182
ans(:,:,2) =
0.5677 0.4133
ans(:,:,3) =
0.3689 0.1615
>> mean(mean(A)) % Along dim 1 and 2, but still 3D!
ans(:,:,1) =
0.8049
ans(:,:,2) =
0.4905
ans(:,:,3) =
0.2652
You can actually SQUEEZE singleton dimensions if/when needed:
>> squeeze(mean(mean(A)))
ans =
0.8049
0.4905
0.2652
but you could also permute dimensions:
>> mean(mean(permute(A, [3,1,2]), 3), 2)
ans =
0.8049
0.4905
0.2652
or
>> mean(mean(permute(A, [1,3,2]), 3))
ans =
0.8049 0.4905 0.2652
So .. no, it was not completely trivial ;-)
  2 Comments
Cedric
Cedric on 19 Mar 2013
My pleasure. Just have a look again at the 1st sentence please; I think that I reworded it while you were posting your comment, so you might not have seen the update!

Sign in to comment.

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!