Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a 2D array,

data = {{0,1}, {1,2}, {2,3}};

I would like to convert it such that I get the x- and y-values separately, specifically

x[0]=0; x[1]=1; x[2]=2;
y[0]=1; y[1]=2; y[2]=3;

Is there a way to obtain this?

share|improve this question
You could do it in many differernt ways however the most appropriate tool for this task is Scan, this is a duplicate of define a function from a list – Artes Aug 5 '14 at 11:24
@Artes This question is a little bit different than the linked question because here we have to iterate the x[i] (i.e. we have to know where we are in the list during scanning). Anyway here's my first attempt at using Scan: Scan[{x[#[[3]]]=#[[1]],y[#[[3]]]=#[[2]]}&,MapIndexed[Join,data]]. If anyone has a better implementation I'd love to hear them. – seismatica Aug 5 '14 at 14:28
up vote 2 down vote accepted

Update: Öskå informs me that what you wanted is not in fact what you asked for. Next time ask the right question to get the right answer. Output formatting is often completely different and if that is your goal you need to clearly state it.

f[v_, {r_, c_}] := HoldForm[#[#2] = v;] &[{"x", "y"}[[c]], r - 1]

out = MapIndexed[f, data, {2}]\[Transpose] // Grid
x[0]=0; x[1]=1; x[2]=2;
y[0]=1; y[1]=2; y[2]=3;

You can use Copy As > Plain Text to copy that output, or you can export it like this:

Export["file.txt", ToString[out, StandardForm]]

I don't see the point of making the assignments that you describe.
Part is a fast operation so you might simply use that:

x[n_] := data[[n + 1, 1]]
y[n_] := data[[n + 1, 2]]

With different data to make the example more clear:

data = {{a, b}, {c, d}, {e, f}};

Array[x, 3, 0]
Array[y, 3, 0]
{a, c, e}

{b, d, f}
share|improve this answer
@Öskå Noted. A very poorly written question in that case. :-/ – Mr.Wizard Aug 5 '14 at 18:19
Indeed, but I personally didn't bother trying to understand it. I assumed that the OP knew about Part and that there was something behind (his profile talks about C/C++). – Öskå Aug 5 '14 at 18:20

Probably not the prettiest but here you go:

data = {{0, 1}, {1, 2}, {2, 3}};
(x[#] = (First /@ data)[[#2]]) & @@@ 
  Thread@{Range[0, Length[First /@ data] - 1], Range[Length[First /@ data]]};
(y[#] = (Last /@ data)[[#2]]) & @@@ 
  Thread@{Range[0, Length[Last /@ data] - 1], Range[Length[Last /@ data]]};


Mathematica graphics

share|improve this answer
The semicolon is missing, can that be added? – BillyJean Aug 5 '14 at 10:57
I don't see how. What's the purpose of this? You could just treat it as a string if you need it. – Öskå Aug 5 '14 at 10:58
I need the data for C, so there has a be a semicolon. Otherwise I have to add it manually, but its a large data set. – BillyJean Aug 5 '14 at 11:01
Added the semicolon myself by stringjoin – BillyJean Aug 5 '14 at 11:18
@BillyJean Great, like I said, it all depends on what you wanted to do with it :) – Öskå Aug 5 '14 at 11:48


MapThread[Set, {{x[#], y[#]} & /@ Range[0, Length@data - 1], data}];

Mathematica graphics

share|improve this answer
+1 however this will fail to reassign values. Also, the OP wanted to index from zero. – Mr.Wizard Aug 5 '14 at 17:56
@Mr.Wizard could you help me understand how my method fails to reassign value? Thanks for the upvote! BTW I need help with belisarius' answer on another Q&A that came from one of your answers (ref]). Could you point me to the right Q&A or help me understand what's going on in that piece of code? – seismatica Aug 5 '14 at 21:56
I mean that if e.g. x[1] already has a value it will evaluate before Set sees it, so you'll be doing effectively 1 = 2 or whatever. You would need to hold that expression somehow. If you cannot see a solution for that, and you are interested, post this code in a Question asking how it may be modified to allow reassignment for existing definitions. – Mr.Wizard Aug 5 '14 at 22:27

Use data[[All,1]] for all x coordinates and data[[All,2]] for all y coordinates

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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