フィルターのクリア

Tricky formula with cumulative sum

4 ビュー (過去 30 日間)
Peta
Peta 2014 年 9 月 23 日
コメント済み: Peta 2014 年 9 月 24 日
I’m trying to calculate a ”standard deviation series” with a formula that involves a cumulative sum, but my matlab skills apparently aren’t sufficient to figure out how to type the formula in matlab.
The equation I want to write can be seen in step 5. Here: http://en.wikipedia.org/wiki/Rescaled_range
I have a vector “X” and a vector “u” and if I just use the cumulative sum command matlab doesn’t interpret the sum in the way I want it. This is how I want the calculation to look like:
Y(1) = ( X(1)-u(1) )^2
Y(2) = ( X(1)-u(2) )^2 + ( X(2)-u(2) )^2
Y(3) = ( X(1)-u(3) )^2 + ( X(2)-u(3) )^2+ ( X(3)-u(3) )^2
Y(4) = ( X(1)-u(4) )^2 + ( X(2)-u(4) )^2+ ( X(3)-u(4) )^2+ ( X(4)-u(4) )^2
….and so on
So in other words the calculation is increasing in size for each step and keeps X(1), X(2), X(3) and so on but only uses the most current value for “u” in each of the calculations.
This should be pretty simple right? How do I make it happen in matlab, any ideas?
Thanks
  3 件のコメント
Star Strider
Star Strider 2014 年 9 月 23 日
It looks like the summation inside the radical in ‘Step 5’.
Peta
Peta 2014 年 9 月 24 日
Yes exactly, I just wrote the (x(i)-u)^2 summation part, then it should be multiplied with 1/t and square rooted. Or do you think I interpreted the formula wrong?

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

採用された回答

Roger Stafford
Roger Stafford 2014 年 9 月 24 日
編集済み: Roger Stafford 2014 年 9 月 24 日
To use 'cumsum' you need to get the u's out of the squared expression. Let's assume X and u are row vectors.
Y = cumsum(X.^2)-2*cumsum(X).*u+(1:size(u,2)).*u.^2;
  1 件のコメント
Peta
Peta 2014 年 9 月 24 日
Aah, beautiful! That did exactly what I wanted it to! I would never have figured that out myself, thanks!

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

その他の回答 (1 件)

Star Strider
Star Strider 2014 年 9 月 23 日
This seems to do what you want:
X = randi(20,1,10); % Create ‘X’
u = 1:10; % Create ‘u’
for k1 = 1:10
Y(k1) = sum((X(1:k1)-u(k1)).^2);
end
At least it produces the correct result (checked with manual calculation).

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by