This is a great place to use the diff function.
Your first step will be this: B = [0 diff(a)]
The reason for adding 0 is to keep the matrix of equal length due to how the diff function works. It will start with the first element in the matrix, and then report the difference between this and the next element. There is no leading element before the first, so it just truncates the matrix with one element. We add zero because there are no changes, since this is the initial element.
If you look at the results in B , itβs now completely obvious where the inflection points are (where you go from positive to negative).
To do this programmatically, there are a number of things you can do. I usually use a little multiplication and the find .
Result = find(B(1:end-1).*B(2:end)<0)
This will return the index where you are at the top of the inflection. In this case, it will be:
ans = 4 7 13
source share