Message Boards Message Boards

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

{2} in Position output instead of the number 2

Posted 10 years ago

Hi, I'm using the Position function to get the position of a number in a matrix, but it prints it as {2} instead of 2 and I can't use it for an index later...

how can i switch it to 2 without the {}??

Please help.

POSTED BY: Christian Rafael
3 Replies

The Position function returns a List of positions. This is because an element might be found in multiple locations on a list.

Position is very often used with First or Last to get the first or last element out of a list.

First[Position[....]]

This is effectively the same code as posted above.

POSTED BY: Sean Clarke
Posted 10 years ago

One way is the following:

x = {2}
x[[1]]
POSTED BY: Jim Baldwin
Posted 10 years ago

Like Sean mentioned you can use First[Position[...]] or FirstPosition[...] to get the first position of what you look for.

To get element from result, you could do:

p = FirstPosition[{a, b, a, a, b, c, b}, b]

Extract[{a, b, a, a, b, c, b}, p]

or

{a, b, a, a, b, c, b}[[First[p]]]
POSTED BY: Jaebum Jung
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