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.
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.
One way is the following:
x = {2}
x[[1]]
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.
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]]]