1

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?

BillyJean
  • 1,273
  • 7
  • 25
  • 2
    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 05 '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 05 '14 at 14:28

4 Answers4

2

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]]};

?x

Mathematica graphics

Öskå
  • 8,587
  • 4
  • 30
  • 49
2

One-liner:

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

Mathematica graphics

seismatica
  • 5,101
  • 1
  • 22
  • 33
  • +1 however this will fail to reassign values. Also, the OP wanted to index from zero. – Mr.Wizard Aug 05 '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 05 '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 05 '14 at 22:27
2

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}

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 1
    See http://mathematica.stackexchange.com/questions/56634/converting-format-of-list#comment162436_56636 – Öskå Aug 05 '14 at 18:12
  • 1
    @Öskå Noted. A very poorly written question in that case. :-/ – Mr.Wizard Aug 05 '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 05 '14 at 18:20
1

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

molekyla777
  • 2,888
  • 1
  • 14
  • 28