Im not sure on how i’m supposed to do this. Kinda new to Mathematica. Also how would I produce a sequence that would go something like [x, y, 2x, 2y, 3x, 3y, 4x ,4y,…]
In[6]:= Flatten[Table[{x, y}*n, {n, 5}]]
Out[6]= {x, y, 2 x, 2 y, 3 x, 3 y, 4 x, 4 y, 5 x, 5 y}
There are multiple ways of doing this.
One possibility is to first construct these {n x,n y} pairs using Table:
t = Table[ {n*x,n*y}, {n,4}] (* gives you {{x,y},{2x,2y},{3x,3y},{4x,4y}} *)
Then you can Flatten that list:
Flatten[t] (* gives you {x,y,2x,2y,3x,3y,4x,4y} *)
In[12]:= Flatten[Transpose @ Outer[Times, {x, y}, Range[5]]]
Out[12]= {x, y, 2 x, 2 y, 3 x, 3 y, 4 x, 4 y, 5 x, 5 y}
There are many different ways of doing this, for the case of 2 variables (x and y) the most elegant way is using Riffle:
Riffle[Range[m] x, Range[m] y]
where m is the length of the integers. There are several possibilities:
Flatten[Table[{x, y}*n, {n, m}]]
Flatten[Table[{n*x, n*y}, {n, m}]]
Flatten[Transpose[{x Range[m], y Range[m]}]]
RepeatedTiming[Flatten[Outer[Times, Range[m], {x, y}]];]
Riffle[Range[m] x, Range[m] y]
We can test which is the fastest given a certain m:
ClearAll[GetTiming]
GetTiming[m_] := Module[{out},
out = {RepeatedTiming[Flatten[Table[{x, y}*n, {n, m}]];],
RepeatedTiming[Flatten[Table[{n*x, n*y}, {n, m}]];],
RepeatedTiming[Flatten[Transpose[{x Range[m], y Range[m]}]];],
RepeatedTiming[Riffle[Range[m] x, Range[m] y];],
RepeatedTiming[Flatten[Outer[Times, Range[m], {x, y}]];]
}[[All, 1]];
{m, #} & /@ out
]
all = GetTiming /@ DeleteDuplicates[Round[PowerRange[1, 10000, 1.5]]];
ListLogLogPlot[Transpose[all], PlotLegends -> Automatic, Joined -> True, PlotRangePadding -> Scaled[.05], Frame -> True]
giving:
@Arnoud Martijn Buzing ’s, @Frank Kampas’s and my solutions are tied. Now it comes down to conceptionally the easiest and generality, which is considered better…
Note that I optimized Frank’s code…
Not using the distributive property, like you do, as opposed to Frank, is a bit faster actually… interesting!
In[5]:= Flatten[#*{x, y} & /@ Range[5]]
Out[5]= {x, y, 2 x, 2 y, 3 x, 3 y, 4 x, 4 y, 5 x, 5 y}
One can continue ad infinitum, using Array, pure functions, Map, Table, Do (with Sow and Reap), and using Join@@ instead of Flatten (or even Cases[#,_,{2}]&), or using Tuples like this:
Times @@@ Tuples[{Range[10], {x, y}}]
There will be at least 50 ways of doing it, and that is on the conservative side ![]()
I think it is instructive to see the various ways it can be done.
m=4;
Flatten[Table[{x,y}n,{n,m}]]
Flatten[Table[{x,y}n,{n,Range[m]}]]
Flatten[Table[{n x,n y},{n,m}]]
Flatten[Table[{n x,n y},{n,Range[m]}]]
Flatten[Table[n v,{n,m},{v,{x,y}}]]
Flatten[Table[n v,{n,Range[m]},{v,{x,y}}]]
Flatten[Transpose[{x Range[m],y Range[m]}]]
Flatten[#{x,y}&/@Range[m]]
Flatten[{# x,# y}&/@Range[m]]
Flatten[Range[m]#&/@{x,y},{2,1}]
Flatten[Outer[Times,Range[m],{x,y}]]
Flatten[Reap[Do[Sow[n{x,y}],{n,m}]][[2]]]
Flatten[Reap[Do[Sow[n{x,y}],{n,Range[m]}]][[2]]]
Flatten[Reap[Do[Sow[n v],{n,m},{v,{x,y}}]][[2]]]
Flatten[Reap[Do[Sow[n v],{n,Range[m]},{v,{x,y}}]][[2]]]
Flatten[Transpose[Partition[Reap[Do[Sow[n v],{v,{x,y}},{n,Range[m]}]][[2,1]],m]]]
Join@@Table[{x,y}n,{n,m}]
Join@@Table[{x,y}n,{n,Range[m]}]
Join@@Table[{n x,n y},{n,m}]
Join@@Table[{n x,n y},{n,Range[m]}]
Join@@Table[n v,{n,m},{v,{x,y}}]
Join@@Table[n v,{n,Range[m]},{v,{x,y}}]
Join@@Transpose[{x Range[m],y Range[m]}]
Join@@(#{x,y}&/@Range[m])
Join@@({# x,# y}&/@Range[m])
Join@@(Outer[Times,Range[m],{x,y}])
Join@@Reap[Do[Sow[n{x,y}],{n,m}]][[2,1]]
Join@@Reap[Do[Sow[n{x,y}],{n,Range[m]}]][[2,1]]
Join@@Reap[Do[Sow[n v],{n,m},{v,{x,y}}]][[2]]
Join@@Reap[Do[Sow[n v],{n,Range[m]},{v,{x,y}}]][[2]]
Join@@Transpose[Partition[Reap[Do[Sow[n v],{v,{x,y}},{n,Range[m]}]][[2,1]],m]]
Cases[Table[{x,y}n,{n,m}],,{2}]
Cases[Table[{x,y}n,{n,Range[m]}],,{2}]
Cases[Table[{n x,n y},{n,m}],,{2}]
Cases[Table[{n x,n y},{n,Range[m]}],,{2}]
Cases[Table[n v,{n,m},{v,{x,y}}],,{2}]
Cases[Table[n v,{n,Range[m]},{v,{x,y}}],,{2}]
Cases[Transpose[{x Range[m],y Range[m]}],,{2}]
Cases[(#{x,y}&/@Range[m]),,{2}]
Cases[({# x,# y}&/@Range[m]),,{2}]
Cases[(Outer[Times,Range[m],{x,y}]),,{2}]
Cases[Reap[Do[Sow[n{x,y}],{n,m}]][[2,1]],,{2}]
Cases[Reap[Do[Sow[n{x,y}],{n,Range[m]}]][[2,1]],,{2}]
Cases[Reap[Do[Sow[n v],{n,m},{v,{x,y}}]][[2]],,{2}]
Cases[Reap[Do[Sow[n v],{n,Range[m]},{v,{x,y}}]][[2]],,{2}]
Cases[Transpose[Partition[Reap[Do[Sow[n v],{v,{x,y}},{n,Range[m]}]][[2,1]],m]],_,{2}]
Module[{j={}},Do[AppendTo[j,n v],{n,m},{v,{x,y}}];j]
Module[{b={x,y}},Join@@Array[#1 b[[#2]]&,{m,2}]]
Join@@Module[{b={x,y}},Array[#1 b[[#2]]&,{m,2}]]
Module[{b={x,y}},Flatten[Array[#1 b[[#2]]&,{m,2}]]]
Flatten[Module[{b={x,y}},Array[#1 b[[#2]]&,{m,2}]]]
Riffle[Range[m] x,Range[m] y]
Times@@@Tuples[{Range[m],{x,y}}]
Flatten[Transpose[Partition[Times@@@Tuples[{{x,y},Range[m]}],m]]]
Join@@Transpose[Partition[Times@@@Tuples[{{x,y},Range[m]}],m]]
MapIndexed[#1 Ceiling[#2[[1]]/2] &, Join @@ ConstantArray[{x, y}, m]]
MapIndexed[#1 Round[#2[[1]]/2 + 0.25] &, Join @@ ConstantArray[{x, y}, m]]
As I said, you can do nearly infinite different ways, with slightly different notation and combinations of techniques: pick your favorite!
Thank you very much! This helped me out so much. If i may ask one more question how would i use “Manipulate” to see how the function increases step by step??
Manipulate[Riffle[Range[m] x, Range[m] y], {m, 1, 10, 1}]
