Message Boards Message Boards

0
|
7011 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Determine the position of the smallest positive number in a list

Posted 9 years ago

How would I determine the position of the smallest positive number (including zero) from a list?

A list of numbers, e.g. Range[-5, 5, 1]

In this case, the list looks like this: {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}

And so the position is 6, which is the value I want returned.

I found some complicated examples on Stack Exchange but have thus far been unable to apply them to this simpler case.

POSTED BY: Greg
3 Replies
Posted 9 years ago
Last[Last[Position[list, Min[Select[list, NonNegative]]]]]

for just the value- thanks

POSTED BY: Greg
Position[list, Min[Select[list, NonNegative]]]
POSTED BY: Ilian Gachevski
Posted 9 years ago

Nevermind, solved it.

a = Range[-5, 5, 1] /. x_ /; x < 0.0 -> 999999999
b = Min[a]
position = Last[Last /@ Position[a, _?(# == b &)]]
POSTED BY: Greg
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract