2.5 Insert a column into a matrix

The problem is to insert a column into the second column position in a 2D matrix.

Mathematica

mat = {{1, 2, 3}, 
       {4, 5, 6}, 
       {7, 8, 9}}; 
 
mat = Insert[Transpose[mat], 
         {90, 91, 92}, 2]; 
mat = Transpose[mat]
 

 {{1,90,2,3}, 
  {4,91,5,6}, 
  {7,92,8,9}}
 

 

Matlab

A=[1 2 3; 
   4 5 6; 
   7 8 9]; 
 
A=[A(:,1)  [90 91 92]'   A(:,2:end) ]
 

     1    90     2     3 
     4    91     5     6 
     7    92     8     9
 

 

Fortran

program t4  implicit none 
  integer(4) ::i 
  integer(4), ALLOCATABLE ::A(:,:) 
 
  A = reshape ([1,2,3,   & 
                4,5,6,   & 
                7,8,9], [3,3], order=[2,1]) 
 
  A = reshape ([A(:,1), [90,91,92] , 
                  A(:,2:)] , [3,4]) 
 
  do i=LBOUND(A, 1),UBOUND(A, 1) 
     print *,A(i,:) 
  end do 
 
end program t4
 

>gfortran t4.f90 
>./a.out 
 1          90           2           3 
 4          91           5           6 
 7          92           8           9
 

 

Maple

A:=< <1|2|3>,<4|5|6>,<7|8|9>>; 
A:=< A(..,1)|<90,91,92>|A(..,2..)>;
 

Using Matrix/Vector

A:=Matrix([ [1,2,3],[4,5,6],[7,8,9]]); 
A:=Matrix([ [A[..,1], 
            Vector[column]([91,92,92]), 
            A[..,2..] ]]);
 

[ 1    90     2     3 
  4    91     5     6 
  7    92     8     9]
 

 

Python

import numpy as np 
mat=np.array([[1,2,3], 
              [4,5,6], 
              [7,8,9]]) 
 
mat=numpy.insert(mat,1,[90,91,92],1)
 

array([[ 1, 90,  2,  3], 
       [ 4, 91,  5,  6], 
       [ 7, 92,  8,  9]])