开发者

Fortran 90 rank mismatch in attempting to extract a vector from an array

In my Fortran 90 code, I have created the following array (called array) of integers:

 1     2     3     4     5     6     7     8     9    10
11    12    13    14    15    16    17    18    19    20
21    22    23    24    25    26    27    28    29    30
31    32    33    34    35    36    37    38    39    40

I wish to extract the first column, and save it in a four-element vector called time. I have the following code:

PROGRAM test
  IMPLICIT NONE
  INTEGER, PARAMETER :: numrows=4, numcols=10
  INTEGER :: i, j, k
  INTEGER, DIMENSION(:,:), ALLOCATABLE :: array, time

  ALLOCATE(array(numrows,numcols))
  ALLOCATE(time(numrows))

  k=1
  DO i=1,numrows
    DO j=1,numcols
      array(i,j)=k
      k=k+1
    开发者_高级运维END DO
  END DO

  DO i=1,numrows
    WRITE(*,"(100(3X,I3))") (array(i,j), j=1,numcols)
  END DO

  time=array(:,1)
END PROGRAM test

But, I get the following error message (when compiling in gfortran):

test.f90:8.15:

  ALLOCATE(time(numrows))
               1
Error: Rank mismatch in array reference at (1) (1/2)
test.f90:22.2:

  time=array(:,1)
  1
Error: Incompatible ranks 2 and 1 in assignment at (1)

Why is this the case? The error message seems to suggest that the array array(:,1) is of rank 2, not rank 1. Is there any way that I can convert array(:,1) to an array of rank 1? Do I need to use RESHAPE to somehow squeeze the array? Or is the problem that by using array(:,1), I am specifying a column vector rather than a row vector? Thank you very much for your time.


You are specifying a rank-2 allocatable array called time:

INTEGER, DIMENSION(:,:), ALLOCATABLE :: array, time

and then attempting to allocate it as a rank-1 array:

  ALLOCATE(time(numrows))

-- don't do that. This works perfectly fine:

PROGRAM test
  IMPLICIT NONE
  INTEGER, PARAMETER :: numrows=4, numcols=10
  INTEGER :: i, j, k
  INTEGER, DIMENSION(:,:), ALLOCATABLE :: array
  INTEGER, DIMENSION(:), ALLOCATABLE :: time

  ALLOCATE(array(numrows,numcols))
  ALLOCATE(time(numrows))

  k=1
  DO i=1,numrows
    DO j=1,numcols
      array(i,j)=k
      k=k+1
    END DO
  END DO

  DO i=1,numrows
    WRITE(*,"(100(3X,I3))") (array(i,j), j=1,numcols)
  END DO

  time=array(:,1)
END PROGRAM test
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜