How to find a value that fits algorithm?

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!

 Accepted Answer

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 Comments

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.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 4 Aug 2016

Commented:

on 4 Aug 2016

Community Treasure Hunt

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

Start Hunting!