How to insert a column into a matrix, the correct Mathematica way

前端 未结 5 1396
旧时难觅i
旧时难觅i 2021-01-30 14:40

I think Mathematica is biased towards rows not columns.

Given a matrix, to insert a row seems to be easy, just use Insert[]

(a = {{1, 2, 3},         


        
5条回答
  •  不思量自难忘°
    2021-01-30 15:23

    I usually just do like this:

    In: m0 = ConstantArray[0, {3, 4}]; 
        m0[[All, {1, 3, 4}]] = {{1, 2, 3}, {4, 0, 8}, {7, 8, 0}}; 
        m0[[All, 2]] = {97, 98, 99}; m0 
    
    Out: 
        {{1, 97, 2, 3}, {4, 98, 0, 8}, {7, 99, 8, 0}}
    

    I don't know how it compare in terms of efficiency.

提交回复
热议问题