why is there a big difference between trapz and mean

4 views (last 30 days)
Hello together, I have the following Problem:
my 2D Vector of a measured Temperatur is T_ZA1.
I Need the mean value, but if I calculate the mean value with:
T_mean_=mean(mean(T_ZA1))
the value is: 121.145.
When I calulate the mean value with trapz like:
T_mean_int=trapz(trapz(T_ZA1,2))/length(T_ZA1(:,1))/length(T_ZA1(:,:));
the value is 47.0566.
Why is this such a big difference? It is not plausible.
Can someone help me please?
Best regards

Accepted Answer

Rik
Rik on 4 Jun 2018
You need to use some tricks to use trapz to estimate a mean. Specifically, you need to divide by the distance between the first and last x-value. I this case you don't supply an x-vector, so Matlab assumes 1:size(Y,dim). It turns out you need to divide by one less than each dim size, so the code below wil get close to mean.
T_mean_=mean(T_ZA1(:))
T_mean_int=trapz(trapz(T_ZA1,2))/prod(size(T_ZA1)-1)
You should really watch out with using multiple divisions without parentheses, and you should be careful with length: it does max(size(A)), which is not always what you want. You should use the second input to size if you apply it on matrices, and you should read up on numel, which is often the intended replacement. The reason I didn't use numel here, is because you need to subtract 1 from each dimension separately.

More Answers (2)

Torsten
Torsten on 4 Jun 2018
Edited: Torsten on 4 Jun 2018
See what happens:
function main
A=[2 5; 3 6];
m11 = mean(A,1)
m12 = mean(m11)
m21 = trapz(A,1)
m22 = trapz(m21)
Best wishes
Torsten.

GUY Shareman
GUY Shareman on 4 Jun 2018
thank you all guys!

Tags

Community Treasure Hunt

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

Start Hunting!