0
$\begingroup$

This question already has an answer here:

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?

$\endgroup$

marked as duplicate by Mr.Wizard Jul 21 '13 at 15:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ 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}}] $\endgroup$ – Mr.Wizard Jul 21 '13 at 15:21
  • $\begingroup$ @Kuba I'm actually fond of that method, and you will see that I included it here. $\endgroup$ – Mr.Wizard Jul 21 '13 at 15:29
  • $\begingroup$ I agree.. It's duplicated. $\endgroup$ – Murta Jul 21 '13 at 17:36
  • $\begingroup$ @Mr.Wizard My comment was addressed to Murta and posted before I've realised your link :) $\endgroup$ – Kuba Jul 21 '13 at 17:47