a=[2 3 6 7 2 1 0.01 6 8 10 12 15 18 9 6 5 4 2].
Here is an array i need to extract the exact values where the increasing and decreasing trend starts.
the output for the array a will be [2(first element) 2 6 9]
a=[2 3 6 7 2 1 0.01 6 8 10 12 15 18 9 6 5 4 2].
^ ^ ^ ^
| | | |
Kindly help me to get the result in MATLAB for any similar type of array..
This is a great place to use the
difffunction.Your first step will be to do the following:
B = [0 diff(a)]The reason we add the 0 there is to keep the matrix the same length because of the way the
difffunction works. It will start with the first element in the matrix and then report the difference between that and the next element. There’s no leading element before the first one so is just truncates the matrix by one element. We add a zero because there is no change there as it’s the starting element.If you look at the results in
Bnow it is quite obvious where the inflection points are (where you go from positive to negative numbers).To pull this out programatically there are a number of things you can do. I tend to use a little multiplication and the
findcommand.Result = find(B(1:end-1).*B(2:end)<0)This will return the index where you are on the cusp of the inflection. In this case it will be: