Hi all,
I'm trying to create a program to find a specific data value. I have a list of a bunch of numbers, and I want to find the first one after which there are four increasing numbers, then, in the next five numbers, no two decreases consecutively. Example: 1 2 3 4 5 6 5 6 7 would return 1. Mostly, my problem is handling the indices on this (getting it to shift one number down every time), and the different loops. Any help appreciated!

 채택된 답변

Guillaume
Guillaume 2016년 8월 4일

0 개 추천

This is how I would do it:
v = [1 2 3 2 1 2 3 4 5 4 3 1 2 3 4 5 4 5 1 2 3 4 5 6 5 6 ]; %sequence of interest is at index 12 (and 19 and 20)
sdv = sign(diff(v)); %find direction of change. 1 = increase, -1 = decrease, 0 = no change
increaseidx = strfind(sdv, [1 1 1 1]); %strfind also works for finding sequences of numbers
decreaseidx = strfind(sdv, [-1 -1]);
%we want indices that have increase but NO decrease 5 indices later
sequenceidx = increaseidx(~ismember(increaseidx+5, decreaseidx))
Certainly loops are not required

댓글 수: 3

Maggy
Maggy 2016년 8월 4일
Thanks for the answer! Is there a way to look at the five numbers after the first four, and make sure the decrease isn't at any point in them (not just at the end)? If I put (decreaseidx,increaseidx+5), problems start to happen.Thanks so much!
sequenceidx = increaseidx(~any(ismember(increaseidx+5 : increaseidx+9, decreaseidx)))
should do the trick.
Maggy
Maggy 2016년 8월 4일
THanks!

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2016년 8월 4일

댓글:

2016년 8월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by