Loops and conditional statement problem

I've been trying to implement a set of loops and conditionals to carry out the following operation:
if sample(i+1)-sample(i) > value if this statement is true FOR AT LEAST 5 consecutive samples then fill another pre-allocated vector with the sample from point where true up until the point where not true.
Thanks for the help.

5 件のコメント

madhan ravi
madhan ravi 2018 年 7 月 16 日
Can you show the example ?
Tom Shemeld
Tom Shemeld 2018 年 7 月 16 日
Var1 is a 1D variable with data that is N samples long.
Allocate vector full of zeros to be filled: => Var2 = zeros(length(Var1),1);
Initiate loop (my coding is quite poor):
for i=1:length(Var1) if Var1(i+1)-Var1(i)>10 _
then if statement above is true for at least 5 consecutive samples. Take the sample where this condition is first true and increment until Var1(i+1) - Var(i) < 10 and place in Var2.
J. Alex Lee
J. Alex Lee 2018 年 7 月 23 日
A simple way to get as far as flagging positions where your condition is met without looping:
dVar1 = diff(Var1) % find differences between successive entries
flag = dVar1>10 % flag the positions where the difference is greater than 10
This gives you a logical array of length N-1, and now you have the problem of finding consecutive true's of length longer than 5...i found this interesting answer elsewhere: https://www.mathworks.com/matlabcentral/answers/366126-how-many-consecutive-ones
Matt C
Matt C 2018 年 7 月 25 日
To build off the answer above, given the flag variable of logicals, this pseudo-code below could potentially solve your problem for actually copying over the parts of the vector that satisfy the condition.
% Track if the last seen element was zero.
lastSeenWasZero = true;
% Track how many consecutives ones have been seen.
oneCounter = 0;
for i = 1:length(flag)
% If last seen element was zero and current element is one, then save the beginning index of the sequence
if lastSeenWasZero && flag(i)
startSubsequence = i
end
% If the current element is one, then increment a counter of how many consecutive ones have been seen.
if flag(i)
lastSeenWasZero = false;
oneCounter = oneCounter + 1
end
% If element is zero, check if at least last 5 elements were ones. If so, then copy the elements which satisfied the condition over from one array to another.
if ~flag(i)
lastSeenWasZero = true;
if oneCounter >= 5
Var2(startSubsequence:startSubsequence+oneCounter) = Var1(startSubsequence:startSubsequence+oneCounter)
end
oneCounter = 0
end
end
%After hitting the end of the flags array, need to check if there were many ones all at the end of the vector.
if oneCounter >= 5
Var2(startSubsequence:startSubsequence+oneCounter) = Var1(startSubsequence:startSubsequence+oneCounter)
end
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 7 月 25 日
The question seems very easy, more clarification needed.

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2018 年 7 月 16 日

コメント済み:

2018 年 7 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by