2
$\begingroup$

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}.

$\endgroup$
6
$\begingroup$

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.

$\endgroup$
3
$\begingroup$
pckpstn = Pick[Range[Length[#] - 1], Negative@Differences@#] &;
lst = {3, 1, 2, 5, 2, 1, 9, 1};
pckpstn@lst
(* {1, 4, 5, 7} *)
$\endgroup$
  • $\begingroup$ Hey, nice to see you answering again! :) $\endgroup$ – rm -rf Mar 29 '14 at 21:00
3
$\begingroup$

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

list = {1, 2, 5, 2, 9};
SparseArray[UnitStep@ListConvolve[{-1, 1}, list]]["NonzeroPositions"]
(* {{3}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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