How to prepend a column to a matrix?

前端 未结 4 1179
情深已故
情深已故 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:29

    Since ArrayFlatten was introduced in Mathematica 6 the least obfuscated solution must be

    matrix = {{1, 2}, {2, 3}}
    vector = {{4}, {5}}
    
    ArrayFlatten@{{vector, matrix}}
    

    A nice trick is that replacing any matrix block with 0 gives you a zero block of the right size.

提交回复
热议问题