数组中的 Fortran 数组

Fortran array inside an array

我想获取一个数组,但是这个数组里面包含了更多的数组。我会举例说明!我想创建 4 个矩阵。主矩阵是E.

   Program array1
   !*************************************************! 
   implicit none

   INTEGER, PARAMETER :: m=2 !rows
   INTEGER, PARAMETER :: n=2 !cols
   Real, DIMENSION(m,n) :: A,D
   Real, DIMENSION(1,2) :: B,C

   REAL, allocatable,DIMENSION(:,:) :: E
   allocate(E(4,4))
   ! Assign values to the matrix
   A(1,:)=(/ 1,  1 /)   
   A(2,:)=(/ 1, 2/)  
   B(1,:)=(/ 1, 2/)   
   C(1,:)=(/ 1,  1 /)   
   D(1,:)=(/ 1,  1 /)   
   D(2,:)=(/ 1,  3 /)  

   !E=(/A, B
   !    C, D)
   ! This shape of array
   !E=[A B
   !   C D]
   !Result should be as under
   !E=[1 1 1 2
   !   1 2 0 0
   !   1 1 1 1
   !   0 0 1 3]
  print *,E
  End program array1

如何在 Fortran 中获取此数组 (E)?我正在研究 f90 和 f95。我创建了一个新数组,它是 array(E)。重要的是获得 E 并且我可以在数组之后进行改进,因为我不知道我将在数组内部使用哪种情况或函数。如果是matlab,很容易,但是fotran我不知道!

我编写了一个函数 catmat,它获取两个二维数组并将它们连接到一个公共维度上。请参阅 EF 以了解其工作原理。你想要的数组是G

请注意分配是在 catmat 内部完成的。

program main
  implicit none
  real, dimension(2,2) :: A
  real, dimension(1,2) :: B
  real, dimension(1,2) :: C
  real, dimension(2,2) :: D
  real, dimension(:,:), allocatable :: E
  real, dimension(:,:), allocatable :: F
  real, dimension(:,:), allocatable :: G
  integer :: i

  A = reshape( [1, 2, 3, 4], [2,2] )
  B(1,:) = [ -1, -2 ]
  C(1,:) = [ -3, -4 ]
  D = reshape( [5, 6, 7, 8], [2,2] )


  write(*,*) ( A(i,:), NEW_LINE('a'), i = 1, size(A,dim=1) )

  write(*,*) ( D(i,:), NEW_LINE('a'), i = 1, size(D,dim=1) )


  E = catmat( A, D, 1)

  write(*,*) "size of E", size(E, dim=1), size(E, dim=2)
  write(*,*) ( E(i,:), NEW_LINE('a'), i = 1, size(E,dim=1) )

  F = catmat( A, D, 2)

  write(*,*) "size of F", size(F, dim=1), size(F, dim=2)
  write(*,*) ( F(i,:), NEW_LINE('a'), i = 1, size(F,dim=1) )

  G = catmat( catmat( A, B, 1), catmat( C, D, 1), 2)

  write(*,*) "size of G", size(G, dim=1), size(G, dim=2)
  write(*,*) ( G(i,:), NEW_LINE('a'), i = 1, size(G,dim=1) )



contains
  function catmat(matl, matr, cdim) result(res)
    real, dimension(:,:), intent(in)  :: matl
    real, dimension(:,:), intent(in)  :: matr
    integer, intent(in)               :: cdim
    real, dimension(:,:), allocatable :: res
    integer                           :: max_dim

    ! Assuming 2d arrays
    if( cdim .ne. 1 .and. cdim .ne. 2 ) then
      write(*,*) "ERROR"
      stop
    end if

    max_dim =  size(matl,dim=cdim)
    if ( size(matr,dim=cdim) .gt. size(matl,dim=cdim) ) max_dim = size(matr,dim=cdim)


    if( cdim .eq. 1 ) then
      allocate( res( max_dim, size(matl,dim=2) + size(matr,dim=2) ) )
    else
      allocate( res( size(matl,dim=1) + size(matr,dim=1), max_dim ) )
    end if

    res = 0

    if(cdim .eq.1 ) then
      res(:,1:size(matl,dim=2)) = matl
      res(:,size(matl,dim=2)+1:size(matl,dim=2)+size(matr,dim=2)) = matr
    else
      res(1:size(matl,dim=1),:) = matl
      res(size(matl,dim=1)+1:size(matl,dim=1)+size(matr,dim=1),:) = matr
    end if

  end function catmat

end program main

尝试使用数组串联操作逐行分配数组 [a,b]

Program array1
!*************************************************! 
implicit none

INTEGER, PARAMETER :: m=2 !rows
INTEGER, PARAMETER :: n=2 !cols
Real, DIMENSION(m,n) :: A,D
Real, DIMENSION(m,n) :: B,C              ! made B,C conforming

REAL, allocatable,DIMENSION(:,:) :: E

    allocate(E(2*m,2*n))
    ! Assign values to the matrix
    A(1,:)=[ 1, 1 ]   
    A(2,:)=[ 1, 2 ]  
    B(1,:)=[ 1, 2 ]   
    C(1,:)=[ 1, 1 ]   
    D(1,:)=[ 1, 1 ]   
    D(2,:)=[ 1, 3 ]

    ! This shape of array
    !E=[A B
    !   C D]

    ! assign row-by-row, using array concat
    E(1,:) = [A(1,:),B(1,:)]
    E(2,:) = [A(2,:),B(2,:)]
    E(3,:) = [C(1,:),D(1,:)]
    E(4,:) = [C(2,:),D(2,:)]

   ! print results row-by-row
    write (*,*) E(1,:)
    write (*,*) E(2,:)
    write (*,*) E(3,:)
    write (*,*) E(4,:)

End program array1

或者,您可以使用数组部分

E(1:2, 1:2) = A
E(1:1, 3:4) = B
E(3:3, 1:2) = C
E(3:4, 3:4) = D