フィルターのクリア

cumsum that restarts whenever it reaches certain level

1 回表示 (過去 30 日間)
Dam
Dam 2014 年 10 月 14 日
コメント済み: Dam 2014 年 10 月 15 日
Good evening I have a matrix A 250*3, i try to do the following: the first time that the cumsum "of raws" is > 0.02 or lower than -0.02, i replace the corresponding value in matrix A with zero and restart the cumsum form the value after the zero. then we repeat this once more: the first time the new cumsum becomes >0.02 or <-0.02 we replace it with zero and restart the cum sum we repeat this untill we arrive to the last raw of the matrix. **with one condition like (cumsum>0.02) i have the following code.. However, i didn't arrive to add the new condition that (cumsum<-0.02).
x = unifrnd(-5,5,[250,2])/100;
cum=cumsum(x);
thresh = 0.02;
while 1
xc = cumsum(x);
k = find(xc > thresh,1,'first');
if isempty(k)
% done
break
end
x(k) = -xc(k-1);
end
Thank you in advance for your help
  2 件のコメント
Image Analyst
Image Analyst 2014 年 10 月 15 日
Why do you need this unusual thing anyway? Are you sure it's really necessary ?
Dam
Dam 2014 年 10 月 15 日
i m developing a portfolio rebalancing method that sells assets whenever they cumulate 2% of losses and buy assets when they gain 2% "cumulative returns" then repeating this along the investment horizon of for example 1 year(252 days). So, i need to know where the cumulative returns reach 2% ou -2% in order to know the points where to buy and sell assets !!

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

採用された回答

Roger Stafford
Roger Stafford 2014 年 10 月 15 日
I think your code only works on one column at a time. It also appears to have the disadvantage that each time your cumulative sum exceeds the threshold, you have to recompute the cumsum for the entire column. I would think a simple for-loop method would be more efficient.
[m,n] = size(A);
for c = 1:n
s = 0;
for r = 1:m
s = s + A(r,c);
if abs(s) > .02
A(r,c) = A(r,c) - s;
s = 0;
end
end
end
  1 件のコメント
Dam
Dam 2014 年 10 月 15 日
Thank you very much it is really simpler to proceed in this way! regards

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by