why is there a big difference between trapz and mean
4 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!