Identify pre-sign changes

조회 수: 2 (최근 30일)
Jan Morawietz
Jan Morawietz 2014년 11월 22일
댓글: John D'Errico 2014년 11월 22일
Assume I have a sequence of +1 and -1 only, randomly chosen..
If I want to identify the sequence point where the pre sign changes, i want to know the number of a second sequence which has the same length...
My code below does not work.. It figures out when the numbers change from positive to negative but after the sequence continues have negative numbers MATKAB still attaches the numbers from the second sequence..
BTW: Im new to MATLAB
thank you for any advice!!!
best Jan
%%%%%%%%%%%%%%%%%%%%%%% xx1 contains only -1 and +1 price contains stock prices which should should be implemented to xx2
for n = 1:length(x1)-1
for k = 2:length(x1)
if xx1(k) ~= xx1(n)
xx2(n) = 0;
else
xx2(n) = price(n);
end
end
end

채택된 답변

Orion
Orion 2014년 11월 22일
I'm not sure to get your problem.
something like
% here changes of sign appear at positions : 3,4,5 and 8
x1 = [1 1 -1 1 -1 -1 -1 1];
% find changes
MySignChangingIndex = find(x1(1:end-1)~=x1(2:end))+1; % gives MySignChangingIndex = [3 4 5 8];
% calculates xx2
xx2 = zeros(size(price));
xx2(MySignChangingIndex) = price(MySignChangingIndex);
  댓글 수: 1
John D'Errico
John D'Errico 2014년 11월 22일
Simpler is just...
find(diff(x1))
which will identify sign changes, since it just searches for any change at all.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Clocks and Timers에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by