Info
This question is closed. Reopen it to edit or answer.
Vectorization
2 views (last 30 days)
Show older comments
Hi,
I have a while loop that takes quite a long time to be calculated (especially when used in combination with ga optimization...I am not sure why) and that I would like to replace with some vectorization.
Here it goes:
while i< end_iteration
if G(i,3)/G(i-lookback_period,6) -1 > threshold_1
s(i: i+ hold_length) = 1;
i = i + hold_length;
elseif G(i,3)/G(i-lookback_period,6) -1 < -threshold_2
s(i: i+ hold_length) = -1;
i = i + hold_length;
else
s(i) = 0;
i = i+1;
end
end
s(1:hold_length)=0;
G is a 7 columns matrix filled with doubles, with around 1million rows. Part of the problem can be solved by doing:
lookback_vector = [zeros(lookback_period,size(G,2));G(lookback_period+1:end,:)];
s(G(:,3)/lookback_vector(:,6)-1 > threshold_1)=1;
s(G(:,3)/lookback_vector(:,6)-1 < -threshold_2)=-1;
s(1:hold_length)=0;
The problem I have then is how to fill with 1s and -1s the value from "i" to "i+hold", also because if say "i_1" < "i_2" <= "i_1 + hold" (with "i_1" and "i_2" being two indices satisfying the if or elseif conditions), then "i_2" shouldn't be a triggering condition.
Any help is appreciated.
Thanks
Answers (1)
Jan
on 27 Dec 2011
cumsum help for such cases usually:
index = find((G(:,3) / lookback_vector(:,6)-1) > threshold_1);
s(index) = 1;
s(index + hold_length) = -1;
s = cumsum(s);
Care for overlapping sections.
3 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!