Prev: label graph vertices
Next: Mathematica 6.0 - Legend
From: zac on 27 Apr 2007 05:30 Dear Group, consider a set of data (y values): data = Table[i*j, {i, 5}, {j, 10}] I want to label each Integer with an x value: label := Table[{i, #} & /@ data[[i]], {i, Length[data]}] I've understand that list creation is much faster with Map than with Table. Is there an effective way to convert the Table to Map in the label function (either with nested Map or to incorporate the Table function into the Map)? Would it be really faster for very large datasets than with Table? thanks Istvan Zachar
From: dh on 28 Apr 2007 05:52 Hi Istvan, here is a Map solution: i=0; ({++i,#})&/@ data However, I can not verify that this is much faster. hope this helps, Daniel zac wrote: > Dear Group, > > consider a set of data (y values): > > data = Table[i*j, {i, 5}, {j, 10}] > > I want to label each Integer with an x value: > > label := Table[{i, #} & /@ data[[i]], {i, Length[data]}] > > I've understand that list creation is much faster with Map than with > Table. > Is there an effective way to convert the Table to Map in the label > function (either with nested Map or to incorporate the Table function > into the Map)? > Would it be really faster for very large datasets than with Table? > > thanks > > Istvan Zachar > >
From: Jean-Marc Gulliet on 28 Apr 2007 05:57 zac wrote: > Dear Group, > > consider a set of data (y values): > > data = Table[i*j, {i, 5}, {j, 10}] > > I want to label each Integer with an x value: > > label := Table[{i, #} & /@ data[[i]], {i, Length[data]}] > > I've understand that list creation is much faster with Map than with > Table. > Is there an effective way to convert the Table to Map in the label > function (either with nested Map or to incorporate the Table function > into the Map)? > Would it be really faster for very large datasets than with Table? > > thanks > > Istvan Zachar > > Istvan, You will not, and cannot, get a straight or definite answer to this kind of question, because the performances depend on the size of the data set but also on its /structure/. You will find below some three functions: label, your original code, label2, a compile version of your original code, and label3, a function that uses MapIndexed to achieve the same functionality than your code (note that label3 is the slowest function in this test). The first data set has few rows (5) and many columns (100,000). The compiled version is faster than the regular version. The second data set has more rows (10) and still many columns (100,000). label2 is still faster than label. The third data set has many rows (100,000) and few columns (10). Both versions are on par. Note that label3 is even slower than during the precedent test. In[1]:= label:=Table[{i,#}&/@data[[i]],{i,Length[data]}] label2=Compile[{{ data,_Integer,2}},Table[{i,#}&/@data[[i]],{i,Length[data]}]]; label3:=Flatten[MapIndexed[{#2[[1]],#1}&,data,{2}],1] In[4]:= Timing[data=Table[i*j,{i,5},{j,10^5}];][[1]] Timing[label; ][[1]] Timing[label2[data]; ][[1]] Timing[label3; ][[1]] Out[4]= 0.531 Second Out[5]= 0.109 Second Out[6]= 0.094 Second Out[7]= 1.328 Second In[8]:= Timing[data=Table[i*j,{i,10},{j,10^5}];][[1]] Timing[label; ][[1]] Timing[label2[data]; ][[1]] Timing[label3; ][[1]] Out[8]= 1.141 Second Out[9]= 0.265 Second Out[10]= 0.235 Second Out[11]= 3. Second In[12]:= Timing[data=Table[i*j,{i,10^5},{j,10}];][[1]] Timing[label; ][[1]] Timing[label2[data]; ][[1]] Timing[label3; ][[1]] Out[12]= 0.235 Second Out[13]= 0.265 Second Out[14]= 0.266 Second Out[15]= 3.344 Second Regards, Jean-Marc
From: dimitris on 28 Apr 2007 05:59 Hi. In[54]:= data = Table[i*j, {i, 5}, {j, 10}] Out[54]= {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}, {4, 8, 12, 16, 20, 24, 28, 32, 36, 40}, {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}} In[68]:= (Partition[Insert[data[[#1]], #1, Partition[Range[10], 1]], 2] & ) /@ Range[5] Out[68]= {{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, {1, 10}}, {{2, 2}, {2, 4}, {2, 6}, {2, 8}, {2, 10}, {2, 12}, {2, 14}, {2, 16}, {2, 18}, {2, 20}}, {{3, 3}, {3, 6}, {3, 9}, {3, 12}, {3, 15}, {3, 18}, {3, 21}, {3, 24}, {3, 27}, {3, 30}}, {{4, 4}, {4, 8}, {4, 12}, {4, 16}, {4, 20}, {4, 24}, {4, 28}, {4, 32}, {4, 36}, {4, 40}}, {{5, 5}, {5, 10}, {5, 15}, {5, 20}, {5, 25}, {5, 30}, {5, 35}, {5, 40}, {5, 45}, {5, 50}}} Dimitris =CF/=C7 zac =DD=E3=F1=E1=F8=E5: > Dear Group, > > consider a set of data (y values): > > data = Table[i*j, {i, 5}, {j, 10}] > > I want to label each Integer with an x value: > > label := Table[{i, #} & /@ data[[i]], {i, Length[data]}] > > I've understand that list creation is much faster with Map than with > Table. > Is there an effective way to convert the Table to Map in the label > function (either with nested Map or to incorporate the Table function > into the Map)? > Would it be really faster for very large datasets than with Table? > > thanks > > Istvan Zachar
From: dkr on 28 Apr 2007 06:05
Istvan, In[14]:= data = Table[i*j, {i, 5}, {j, 10}]; In[15]:= Table[{i,#}&/@data[[i]],{i,Length[data]}]; Your example suggests that you want to give the same label to each integer in a given row of data. If so, then one alternative is: In[16]:= Thread[{#,data[[#]]}]&/@Range[Length[data]] Out[16]= {{{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{ 1,9},{1,10}},{{2,2},{2,4},{2,6},{2,8},{2,10},{2,12},{2,14},{ 2,16},{2,18},{2,20}},{{3,3},{3,6},{3,9},{3,12},{3,15},{ 3,18},{3,21},{3,24},{3,27},{3,30}},{{4, 4},{4,8},{4,12},{4,16},{4,20},{4,24},{4,28},{4,32},{4,36},{ 4,40}},{{5,5},{5,10},{5,15},{5,20},{5,25},{5,30},{5,35},{5,40},{5, 45},{5,50}}} In[17]:= %16===%15 Out[17]= True dkr |