2.4 Insert a row into a matrix

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

Mathematica

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

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

 

Matlab

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

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

 

Maple

Using <<>> notation

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

Using Matrix/Vector

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

[ 1  2   3 
  4  5   6 
  90 91  92 
  7  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],0)
 

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

 

Fortran

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

Compile and run

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