1

I think the question says it all. I have a coordinate pair, {x,y} and I want to extract x and y for use in further calculations. Can't find anything in the docs about this but it seems like it should be a pretty simple operation.

garej
  • 4,865
  • 2
  • 19
  • 42
Keith
  • 49
  • 1
  • 3

3 Answers3

6
{x,y}[[1]]
{x,y}[[2]]

Watch the docs for Part

garej
  • 4,865
  • 2
  • 19
  • 42
Kay
  • 1,035
  • 7
  • 19
6

In addition to answer provided by @Kay:

One of the alternative ways to extract data would be using First and Last functions:

Example:

(*Arbitrary data*)
coord = {76, 2};

(*Process*)
First @ coord
Last @ coord

You could also extract the entire set of x and y values.

Example :

(*Arbitrary data*)
data = RandomInteger[{1, 100}, {10, 2}]

(*All 'x' values*)
data[[All, 1]]

(*All 'y' values*)
data[[All, 2]]

Reference:

@ # % etc.
Part
First
Last

Tutorial:

Getting Pieces of Lists

e.doroskevic
  • 5,959
  • 1
  • 13
  • 32
3

there is another way as well:

#1 & @@ {1, 2} (* gives 1 *)
#2 & @@ {1, 2} (* gives 2 *)

for multiple coordinates in a list:

data = RandomInteger[{1, 100}, {10, 2}]

#1 & @@@ data (* gives all x coordinates *)
#2 & @@@ data (* gives all y coordinates *)
Ali Hashmi
  • 8,950
  • 4
  • 22
  • 42