To specify an array of length L
in Mathematica, where each entry is someElement
, I'm accustomed to writing:
exArray=Array[someElement &, L];
However, let's say that I want to (quickly) generate an array of length L
where there are n
copies of elementOne
and L- n
copies of elementTwo
. Maybe elementOne
is the string character A
and elementTwo
is the string character B
. I'd like to be able to position the two elements in two different ways: (1) where the first n
elements are all elementOne
and the remaining elements are all elementTwo
, and (2) where we have a uniform random sample from the set of all possible arrays where there are n
copies of elementOne
and L - n
copies of elementTwo
.
Are there simple "one-liners" to do (1) + (2)?
ConstantArray
withJoin
for (1) andRandomSample
for (2)? $\endgroup$Array
is more flexible, but in this context, what you have andConstantArray
give the same result. You might find the 3rd argument ofConstantArray
useful if you're initializing sparse arrays. I generally like to write code that "reads well", so if the intent of the line is to generate a constant array, I'll go withConstantArray
. $\endgroup$