CUMMEAN along elements of 3D matrix

4 ビュー (過去 30 日間)
Vesp
Vesp 2017 年 6 月 26 日
コメント済み: Vesp 2017 年 6 月 26 日
I am trying to use cummean in order to take the cumulative mean by element in a (192x144x8766) matrix A.
I have tried cummean(A,3) and cummean(:,:,3) and get difference answers. Which one would be correct for taking the cumulative mean along the elements of a 3D matrix?

採用された回答

Walter Roberson
Walter Roberson 2017 年 6 月 26 日
cummean(:,:,3) would be for accessing a variable along the third dimension, not for calling a function.
You should probably use cummean(A,3) . Except, that is, for the fact that cummean() is not a Mathworks function. There is a cummean() in the File Exchange. Or you can implement it yourself:
Acm3 = bsxfun(@divide, cumsum(A, 3), reshape(1:size(A,3), 1, 1, []) );
Or, if you have R2016b or later,
Acm3 = cumsum(A,3) ./ reshape(1:size(A,3), 1, 1, []);
  9 件のコメント
Walter Roberson
Walter Roberson 2017 年 6 月 26 日
"Is there any way to reduce this difference?"
You are doing the equivalent of assuming that
A*f(x1, y1) + B*f(x2, y2) + C %linear combination of values
should equal f(A*x1+B*x2 + C, A*y1+B*y2 + C) %linear combination of positions
for f(x,y) = sqrt(x^2+y^2)
If we simplify with C = 0, and B = 1 - A (that is, that A is the proportion of the way between the two locations), then with that function we can demonstrate that the maximum difference is at A = 1/2, and then the critical points are at A = 1x2 = -x1 and y2 = -y1, which yields a difference between the two functions that works out to sqrt(x1^2 + y1^2) . However, this difference can be made arbitrarily large just by moving x1 and y1. Note that the interpolated position being evaluated at would work out to be f(0,0) -- which is to say that if you allow me to pick the second point as being the reflection of the first point in the X and Y axes, then the point of evaluation would be (0, 0) and if the function is 0 at (0, 0) and your function is the same no matter what the sign of x and y are, such as for your function, then clearly the difference in values would work out to be the same as the function value itself.
For any other point, for that IVT, assuming evaluation half way along the line joining the points, then the difference is
(1/2)*(x1^2+y1^2)^(1/2) + (1/2)*(x2^2+y2^2)^(1/2) - (1/2)*(x1^2+2*x1*x2+x2^2+(y1+y2)^2)^(1/2)
which is definitely not 0.
How can you reduce the difference? By not allowing the second point to be that far from the first point. Which will probably require that you give up your current method of selecting points.
Vesp
Vesp 2017 年 6 月 26 日
Thank you, this has been so insightful. I will have to think of another method of selecting the points. I appreciate all of your patience, time and help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by