1
$\begingroup$

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?

$\endgroup$
2
  • 2
    $\begingroup$ 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 $\endgroup$
    – Artes
    Aug 5, 2014 at 11:24
  • $\begingroup$ @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. $\endgroup$
    – seismatica
    Aug 5, 2014 at 14:28

4 Answers 4

2
$\begingroup$

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}
$\endgroup$
3
  • 1
    $\begingroup$ See mathematica.stackexchange.com/questions/56634/… $\endgroup$
    – Öskå
    Aug 5, 2014 at 18:12
  • 1
    $\begingroup$ @Öskå Noted. A very poorly written question in that case. :-/ $\endgroup$
    – Mr.Wizard
    Aug 5, 2014 at 18:19
  • $\begingroup$ 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++). $\endgroup$
    – Öskå
    Aug 5, 2014 at 18:20
2
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ The semicolon is missing, can that be added? $\endgroup$
    – BillyJean
    Aug 5, 2014 at 10:57
  • $\begingroup$ I don't see how. What's the purpose of this? You could just treat it as a string if you need it. $\endgroup$
    – Öskå
    Aug 5, 2014 at 10:58
  • $\begingroup$ 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. $\endgroup$
    – BillyJean
    Aug 5, 2014 at 11:01
  • $\begingroup$ Added the semicolon myself by stringjoin $\endgroup$
    – BillyJean
    Aug 5, 2014 at 11:18
  • $\begingroup$ @BillyJean Great, like I said, it all depends on what you wanted to do with it :) $\endgroup$
    – Öskå
    Aug 5, 2014 at 11:48
2
$\begingroup$

One-liner:

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

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ +1 however this will fail to reassign values. Also, the OP wanted to index from zero. $\endgroup$
    – Mr.Wizard
    Aug 5, 2014 at 17:56
  • $\begingroup$ @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? $\endgroup$
    – seismatica
    Aug 5, 2014 at 21:56
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Aug 5, 2014 at 22:27
1
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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