gfortran - 2D array concatenation in fortran -
fortran 2003 has square bracket syntax array concatenation, intel fortran compiler supports too. wrote simple code here matrix concatenation:
program matrix implicit none real,dimension (3,3) :: mat1,mat2 real,dimension(3,6):: mat3 integer mat1=reshape( (/1,2,3,4,5,6,7,8,9/),(/3,3/)) mat2=reshape( (/1,2,3,4,5,6,7,8,9/),(/3,3/)) mat3=[mat1,mat2] !display i=1,3,1 write(*,10) mat3(i,:) 10 format(f10.4) end end program
but error
mat3=[mat1,mat2] error: incompatible ranks 2 , 1 in assignment
i expect output as
1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9
can comment going wrong? rank 2 , 1 here? guess arrays have rank 2.
the array concatenation in fortran 2003 doesn't work think. when concatenate, it's not going stack 2 arrays side side. pick elements first array 1 one , put one-dimensional array. same thing second array append 1-d form of first array.
the following code works.
program matrix implicit none real,dimension (3,3) :: mat1,mat2 real,dimension(18) :: mat3 integer mat1=reshape( (/1,2,3,4,5,6,7,8,9/),(/3,3/)) mat2=reshape( (/1,2,3,4,5,6,7,8,9/),(/3,3/)) mat3=[mat1,mat2] print*, shape([mat1,mat2]) !check shape of concatenated array !display i=1,18,1 write(*,10) mat3(i) 10 format(f10.4) end end program
however, result wanted can achieved using following code
program matrix implicit none real,dimension (3,3) :: mat1,mat2 real,dimension(3,6) :: mat3 integer mat1=reshape( (/1,2,3,4,5,6,7,8,9/),(/3,3/)) mat2=reshape( (/1,2,3,4,5,6,7,8,9/),(/3,3/)) i=1,3 mat3(i,:)=[mat1(:,i),mat2(:,i)] enddo !display i=1,3,1 write(*,*) mat3(i,:) end end program
Comments
Post a Comment