How can I get a function that gives the following output?
listX = {a, b, c, d}
numberX = 3
myDuplicatesList[listX, numberX]
{{a,a,a},{b,b,b},{c,c,c},{d,d,d}}
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI like using KroneckerProduct
for problems like this:
KroneckerProduct[listX, ConstantArray[1, numberX]]
{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}
The KroneckerProduct
approach should be much faster than the others for large vectors.
Alternatively:
listX = {a, b, c};
numberX = 3;
Table[ConstantArray[i, numberX], {i, listX}]
{{a, a, a}, { b, b, b}, {c, c, c}}
Or:
listX = {a, b, c, d};
numberX = 3;
Transpose@Table[i, {numberX}, {i, listX}]
Array[listX &, numberX, 1, Transpose[{##}] &]
{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}
ArrayPad[List /@ listX, {0, {0, numberX - 1}}, "Fixed"]
{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}
PadRight[{#}, numberX, "Fixed"] & /@ listX )* or *)
PadRight[List /@ listX, {Automatic, numberX}, "Fixed"]
{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}
TensorProduct[listX, Array[1 &, numberX]]
{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}
ArrayResample[listX, Scaled @ numberX , "Bin", Resampling -> "NearestLeft"]
{a, a, a, b, b, b, c, c, c, d, d, d}
Partition[%, numberX]
{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}
one way might be
listX = {a, b, c, d}
numberX = 3
Transpose[{listX}].{Table[1, {numberX}]}
Transpose@ConstantArray[listX, numberX]
{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}
Yet Another Way:
Flatten[ConstantArray[{listX}, numberX], {2, 3}]
And another (the last argument 1
is necessary only when listX
is not a flat list):
Outer[Times, listX, ConstantArray[1, numberX], 1]
listX = {a, b, c, d};
numberX = 3;
Using SequenceReplace
:
SequenceReplace[listX, {g_} :> Table[g, numberX]]
Using Replace
:
listX // Replace[#, a_ :> Table[a, numberX], {1}] &
{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}