0

What is the fastest way to add a constant (1 in this test) into a array? There is my attempt:

m=RandomInteger[100,{100000,30}];
(test1={##,1}&@@@m)//AbsoluteTiming//First
(test2=Join[m,ConstantArray[{1},Length@m],2])//AbsoluteTiming//First
(test3=Thread@Append[Transpose@m,1])//AbsoluteTiming//First
(test4=Join[#,{1}]&/@m)//AbsoluteTiming//First
(test5=Append[#,1]&/@m)//AbsoluteTiming//First
(test6=Join[m,{0}&/@m,2])//AbsoluteTiming//First
(test7=ArrayFlatten@{{m,0}})//AbsoluteTiming//First
(test8=ArrayPad[m,{{0,0},{0,1}}])//AbsoluteTiming//First
0.304553
0.171397
0.170948
0.054199
0.064244
0.013391
0.010672
0.009288

It's the first way a good answer?

Murta
  • 26,275
  • 6
  • 76
  • 166
  • Murta, I'm closing this because I think it has already been answered. The methods there should be easily adaptable. Please let me know if you find this not to be the case. For your packed array I would use: ArrayFlatten[{{m, 1}}] – Mr.Wizard Jul 21 '13 at 15:21
  • @Kuba I'm actually fond of that method, and you will see that I included it here. – Mr.Wizard Jul 21 '13 at 15:29
  • I agree.. It's duplicated. – Murta Jul 21 '13 at 17:36
  • @Mr.Wizard My comment was addressed to Murta and posted before I've realised your link :) – Kuba Jul 21 '13 at 17:47

0 Answers0