2013-10-29 2 views
2

배열 배열을 정의하려고합니다. 는 I가 정의한 :포트란의 배열 배열

integer,dimension(2,2):: & 
    x=reshape(source= (/0,1,1,0/), shape=(/2,2/)), & 
    y=reshape(source= (/1,0,0,1/), shape=(/2,2/)), & 
    z=reshape(source= (/1,1,1,1/), shape=(/2,2/)) 

는 I, 즉, S (3), 이는, (X/Y/Z)으로되는 구성 요소, 가령 배열을 정의

s(1)=x 
s(2)=y 
and s(3)=z 

원하는 어떻게 수 달성 해?

답변

5

가장 간단한 방법은 아마도 순위-3 배열로

integer, dimension(3,2,2) :: s 

s을 정의 할 수 있습니다 다음과 같은 문장을 작성할 수 있습니다

s(1,:,:) = x 
s(2,:,:) = y 
... 

이것은 '자연스럽게'포트란에 배열 배열을 구현하는 방법입니다. 당신이 더 많은 관심을 끌 수있는 대안은 같은 것입니다 : 당신이 당신의 유형 twodarray의 작동 =을 재정의 할 수 s(1)%elements = x의 말 많음을 좋아하지 않는 경우에

type :: twodarray 
    integer, dimension(2,2) :: elements 
end type twodarray 

type(twodarray), dimension(3) :: s 

s(1)%elements = x 

을, 지금은 시간이 없어 그 코드를 작성할 수 있습니다.

2

당신은 항상 인쇄됩니다 (포트란 95) 포인터

program main 
    implicit none 

    type :: my_type 
    integer, pointer :: my_size(:)  ! F95 
    !integer, allocatable :: my_size(:) ! F95 + TR 15581 or F2003 
    end type my_type 

    type(my_type), allocatable :: x(:) 

    allocate(x(3)) 

    allocate(x(1)%my_size(3)) 
    allocate(x(2)%my_size(2)) 
    allocate(x(3)%my_size(1)) 

    print*, x(1)%my_size 
    print*, x(2)%my_size 
    print*, x(3)%my_size 

    deallocate(x(3)%my_size, x(2)%my_size, x(1)%my_size) 
    deallocate(x) 

end program main 

을 사용할 수 있습니다

 0   0   0 
     0   0 
     0