My question is very simple but I am not able to find the optimal solution. What I am trying to do is to have a function that give me the position in a list at which the value at position i
is bigger than at position i+1.
I need only the first of such term since afterwards, I will commute the two via another a function and then test again if everything is ordered. Basically, I am searching for
In[] : findpos[{1,2,3,4,7,4}]
Out [] : 5
My current solution is the following
findpos[a_] :=
Module[{index = {}},
For[i = 1, i <= Length[a] - 1, i++, If[a[[i]] > a[[i + 1]], index = Append[index, i]]];
Return[First[index]]]
which gives
findpos[{1,2,3,4,7,4}]
5
I will have to call this function maybe many time, so I would like something which run fast and I am almost convinced that a module + a For loop will not be the optimal solution speaking of computation time. I looked at many other questions somehow related ; especially this one How to find the position of elements in a list satisfying criteria
I think I am searching for something of the form
Position[{2, 4, 6, 8, 10}, _?(# > 7 &)]
But who compare two elements instead of each element to a scalar. I tried to adapt it to my case without success. I tried something of the form
Position[{4, 5, 2, 1}, _?(#1 >= #2 &)]
Any suggestion ?
SequencePosition[#,{x_,y_}/; x>y]&@{1, 2, 3, 4, 7, 4}
$\endgroup$