why is there a big difference between trapz and mean

조회 수: 4 (최근 30일)
GUY Shareman
GUY Shareman 2018년 6월 4일
답변: GUY Shareman 2018년 6월 4일
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

채택된 답변

Rik
Rik 2018년 6월 4일
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.

추가 답변 (2개)

Torsten
Torsten 2018년 6월 4일
편집: Torsten 2018년 6월 4일
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 2018년 6월 4일
thank you all guys!

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by