Performance

There is a reason to use the form I proposed over that which Heike gave. If the first argument of PadRight is a packed array, and the padding list is packable (but not necessarily packed) the result will also be packed, and it is produced more quickly than if it were not packed.

The version listed in Heike's answer is slightly shorter than (and not too different from) the one given here...
–
Guess who it is.♦Feb 11 '12 at 23:38

2

I ran AbsoluteTiming[] on every answer given, and this is the one that was the fastest on my system (with x=35). I felt like I had to choose, and well.. I accepted this answer. The difference between Heike's answer and this one is that Mr.Wizard's is a pure function, a concept that I had not encountered before.
–
CHMFeb 12 '12 at 2:38

Unfortunately {a,b} is not a packed array. I tried replacing {a,b} with ab defined as ab = Developer`ToPackedArray[{a, b}];, but PadRight[ab, Length@a, ab] // Developer`PackedArrayQ is False. Is there any way to get the result as a packed array?
–
Karsten 7.Jul 29 at 22:01

@Karsten It seems that the individual elements (a, b) are packed but the outer list is not, in either case. I amended my answer to note this.
–
Mr.Wizard♦Jul 29 at 22:24

Heike I would have deleted my answer but CHM already Accepted it. I have to leave right now but I think "what to do in this situation" is worthy of a Meta post. Sorry. :-/
–
Mr.Wizard♦Feb 11 '12 at 23:55

1

@Mr. Wizard: you do know that moderators are capable of deleting accepted answers, no?
–
Guess who it is.♦Feb 12 '12 at 0:14

1

@Mr. Wizard, as you wish. The situation just doesn't sit well with me, but I leave this to your judgment.
–
Guess who it is.♦Feb 12 '12 at 4:03

@CHM Actually you could replace the second ConstantArray[...] by {b} to simplify things a bit.
–
SzabolcsFeb 11 '12 at 22:56

1

Floor[Length[a]/2] is more conventionally written as Quotient[Length[a], 2].
–
Guess who it is.♦Feb 11 '12 at 22:59

@J.M. Well, conventions change from place to place so it depends who you ask :-) I use that form as well sometimes, but here what I really needed was the Ceiling, and the Floor is more symmetrical with that. Also, I realized the Floor part wasn't necessary as a simple {b} would do as in @Heike's answer ...
–
SzabolcsFeb 11 '12 at 23:08

This is the method I was thinking of, or at least close enough that I don't have to bother putting an answer together.
–
rcollyerFeb 12 '12 at 1:54

@CHM You mean I used loops for this kind of stuff in the book ?! I thought better of myself.
–
Leonid ShifrinFeb 11 '12 at 22:44

Quite the opposite - you suggest NOT to use loops, and make use of Mathematica's functional programming capabilities instead. As I'm learning to use Mathematica, I'd rather learn to code efficiently.
–
CHMFeb 11 '12 at 22:45

@CHM Well, that's a relief. I started thinking I did use loops, which would mean that I wasn't in the right mind when writing that part.
–
Leonid ShifrinFeb 11 '12 at 22:48

1

@CHM In fact, today is an anniversary - it is exactly 3 years that I released the book to the general audience (it was written one year prior to that, but I did not have a web version).
–
Leonid ShifrinFeb 11 '12 at 22:51

Congratulations on being first to 5000! :-)
–
Mr.Wizard♦Feb 12 '12 at 4:32

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.