How to count time steps
이전 댓글 표시
Hi, I have X has several numbers inside say: X = [1 4 5 3 2 7 8 4 6 9 3 2 8 5 3 10] I want to first to select the values where X falls below 4, then need to count how many time steps does it take each one to go above 8.
Hope I am clear Thanks
댓글 수: 2
Image Analyst
2017년 1월 8일
편집: Image Analyst
2017년 1월 8일
So it first goes above 4 at element 3 (where it's 5). Then to go from 5 to 9 (the first time it goes above 8) is 7 steps. Then it goes above 4 at element 14 (where it's 5) then it's 3 steps to get to 10. So is the answer [7, 3]? Or are your rules different than that?
What is the reason why you want this quirky thing?
Karoline Qasem
2017년 1월 9일
채택된 답변
추가 답변 (1개)
Image Analyst
2017년 1월 8일
편집: Image Analyst
2017년 1월 8일
Try this:
% Define vector.
x = [1 4 5 3 2 7 8 4 6 9 3 2 8 5 3 10]
% Plot it.
plot(x, 'b*-', 'LineWidth', 2, 'MarkerSize', 13)
grid on;
hold on;
line(xlim, [4,4], 'Color', 'r', 'LineWidth', 2);
% Do the scan.
startingIndex = 1;
loopcounter = 1;
while startingIndex < length(x)
subvector = x(startingIndex:end);
% Find where it goes above 4 next. Note ABOVE, not above or equal to.
nextAbove4Index = find(subvector > 4, 1, 'first') + startingIndex - 1;
% Find where it goes above 8 next. Note ABOVE, not above or equal to.
nextAbove8Index = find(subvector > 8, 1, 'first') + startingIndex - 1;
% Count the number of steps for that to happen.
numSteps(loopcounter) = nextAbove8Index - nextAbove4Index;
% Move to the next index past the one that exceeded 8.
startingIndex = nextAbove8Index + 1;
loopcounter = loopcounter + 1;
end
numSteps % Report results to command window.

The result is [7, 3] as expected.
카테고리
도움말 센터 및 File Exchange에서 Third-Party Cluster Configuration에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!