How to prepend a column to a matrix?

前端 未结 4 1180
情深已故
情深已故 2020-12-09 12:16

Ok, imagine I have this Matrix: {{1,2},{2,3}}, and I\'d rather have {{4,1,2},{5,2,3}}. That is, I prepended a column to the matrix. Is there an easy way to do it?

My

4条回答
  •  一生所求
    2020-12-09 12:32

    THE... ABSOLUTELY.. BY FAR... FASTEST method to append or prepend a column from my tests of various methods on array RandomReal[100,{10^8,5}] (kids, don't try this at home... if your machine isn't built for speed and memory, operations on an array this size are guaranteed to hang your computer) ...is this: Append[tmp\[Transpose], Range@Length@tmp]\[Transpose]. Replace Append with Prepend at will.

    The next fastest thing is this: Table[tmp[[n]]~Join~{n}, {n, Length@tmp}] - almost twice as slow.

提交回复
热议问题