hungphan
(Hung Phan)
1
Hello,
I want to import a two column table of numeric data to Mathematica and set each number to two vector variables x and y.
For example the below table:
1 0.1
2 0.5
3 0.7
4 12
5 -3
After importing I want to set x[1]=1, x[2]=2…x[5]=5; and y[1]=0.1, y[2]=0.5…y[5]=-3.
I know how to import the table but I can not figure out how to import it and set variable simultaneously.
Would you please help me out? Thanks.
In[1]:= Evaluate[
Table[{x[i], y[i]}, {i, 1, 5}]] = {{1, .1}, {2, .5}, {3, .7}, {4, 12}, {5, -3}}
Out[1]= {{1, 0.1}, {2, 0.5}, {3, 0.7}, {4, 12}, {5, -3}}
In[2]:= x[3]
Out[2]= 3
In[3]:= y[2]
Out[3]= 0.5
Watch out if you do that more than one time. You might look up Clear and Evaluation->Quit Kernel->Local->Quit
sukrause
(Udo Krause)
3
Hi over there,
in the simplest way it is
In[51]:= SetDirectory[NotebookDirectory[]]
Out[51]= "N:\\Udo\\Abt_N"
In[56]:= FilePrint[".\\test\\hungPhan.txt"]
During evaluation of In[56]:=
1 0.1
2 0.5
3 0.7
4 12
5 -3
In[61]:= {x, y} = Transpose[Import[".\\test\hungPhan.txt", "Table"]]
During evaluation of In[61]:= Syntax::stresc: Unknown string escape \h.
During evaluation of In[61]:= Syntax::stresc: Unknown string escape \h.
Out[61]= {{1, 2, 3, 4, 5}, {0.1, 0.5, 0.7, 12, -3}}
In[59]:= x[[4]]
Out[59]= 4
In[60]:= y[[4]]
Out[60]= 12
Regards
Udo.
dlkeith
(David Keith)
4
MapIndexed[] knows the part of the expression its function is acting on:
In[1]:= data = Table[{n, n/10.}, {n, 5}];
In[2]:= f[{xx_, yy_}, {n_}] := (x[n] = xx; y[n] = yy;)
In[3]:= MapIndexed[f, data, {1}];
In[4]:= {x[2], y[2]}
Out[4]= {2, 0.2}
mbohac
(Mike Bohac)
5
This seems easier than that:
Assuming a table “mydata”, then it can be dumped into variables just by selecting the columns:
x = mydata[[All,1]]
y = mydata[[All,2]]