Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am new to Mathematica. I have created a table of real numbers. I want to create a new table that contains the set of values of $i$ such that, in my original table, the $i$th entry is greater than the $(i+1)$th entry.

For example If I had the table {1,2,5,2,9} my new table would be {3}.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Here's a way:

lis = {1, 2, 5, 2, 9};
Flatten[Position[Greater @@@ Partition[lis, 2, 1], True]]
(* {3} *)

Here's an explanation:

Partition will group the elements in twos

Partition[lis, 2, 1]
(* {{1, 2}, {2, 5}, {5, 2}, {2, 9}} *)

Then we use @@@ to apply a Greater at level 1

Greater @@@ Partition[lis, 2, 1]
(* {False, False, True, False} *)

Since 5 > 2, True appears in the third position. We use Position to see where True occurs and we're done.

share|improve this answer
pckpstn = Pick[Range[Length[#] - 1], Negative@Differences@#] &;
lst = {3, 1, 2, 5, 2, 1, 9, 1};
pckpstn@lst
(* {1, 4, 5, 7} *)
share|improve this answer
    
Hey, nice to see you answering again! :) –  rm -rf Mar 29 at 21:00
    
Thank you @rm-rf. –  kguler Mar 29 at 21:47

Here's a way using ListConvolve and SparseArray's properties:

list = {1, 2, 5, 2, 9};
SparseArray[UnitStep@ListConvolve[{-1, 1}, list]]["NonzeroPositions"]
(* {{3}} *)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.