if... then... else?

조회 수: 8 (최근 30일)
Pieter
Pieter 2011년 4월 11일
Hello,
I have the following problem:
  • Assume a dataset [3000,1]
  • We construct a variable ‘signal’, which will have the same size of our dataset. ‘signal’ will be either 1 or -1, based on the evolution of our dataset.
  • At the starting point (1, 1), ‘signal’ will have the value 1. We keep this value until the dataset moves x % below its highest value during the time that ‘signal’ was equal to 1. From that moment on, the variable ‘signal’ will have the value -1.
  • We keep this value until the dataset moves x % above its lowest value during the time that signal was equal to -1. From that moment on, the variable ‘signal’ will have the value 1 again.
  • And so on, and so on...
  • A short example may make my problem more clear:
x = 10%
  • dataset / signal
  • 97 / 1
  • 100 / 1
  • 93 / 1
  • 89 / -1 / dataset moves x % below its highest value (100) during the '1' signal
  • 86 / -1
  • 80 / -1
  • 93 / 1 / dataset moves x % above its lowest value (80) during the '-1' signal
  • 94 / 1
  • 95 / 1
  • 92 / 1
  • 80 -1 / dataset moves x % below its highest value (95) during the '1' signal
  • … …
Can anybody help me with this issue?
Thanks
Pieter
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 4월 11일
I'm pretty sure you'll find an old cssm thread with exactly the same problem; see http://www.mathworks.com/matlabcentral/newsreader/

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

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 4월 11일
Try this solution:
A = [97 100 93 89 86 80 93 94 95 92 80];
% Preallocate
Out = ones(size(A));
% First element is momentarily the maximum
maxA = A(1);
% Positive sequence
pos = true;
% Tolerance of 10%
x = .1;
% Start from the second element (since the first is considered the max)
for ii = 2:numel(A)
% 1
if pos
% If new maximum
if A(ii) > maxA
maxA = A(ii);
% If threshold is broken downwards
elseif A(ii)/maxA-1 < -x
minA = A(ii);
Out(ii) = -1;
pos = false;
end
% -1
else
Out(ii) = -1;
% If new minimum
if A(ii) < minA
minA = A(ii);
% If threshold is broken upwards
elseif A(ii)/minA-1 > x
maxA = A(ii);
Out(ii) = 1;
pos = true;
end
end
end
  댓글 수: 1
Pieter
Pieter 2011년 4월 11일
Thank you very much!
Exactly where I was looking for!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by