I want to call Range[]
with its arguments depending on a condition. Say we have
checklength = {5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}
I then want to call Range[]
13 times (the length of checklength
) and do Range[5]
when checklength[[#]] == 5
and Range[2, 6]
when checklength[[#]] == 6
. If[]
would seem an appropriate way to do it,
Range[If[checklength[[#]] == 5, 5, XXX]]& /@ Range[13]
but I don't know what to put for "XXX", since I need "2,6" there without any brackets. I've tried
Range[If[checklength[[#]]==5, 5, Flatten[{2,6}]]]& /@ Range[13]
but that doesn't help (in fact if you think about it, it shouldn't!). The problem is, I need an unbracketed pair of numbers to be treated as a single argument and I don't know how to do that. I can think of one quite messy solution,
Range[If[checklength[[#]] == 5, 1, 2], If[checklength[[#]] == 5, 5, 6]& /@ Range[13]
but I'd be disappointed if there's not a better way to do it. Even though this does the trick, the general question remains of how to treat unbracketed comma separated numbers as a single item.
If
:checklength /. {5 -> Range@5, 6 -> Range[2, 6]}
$\endgroup$