0

I know that "transpose" in Fortran is an operator which flips a matrix over its diagonal. However, in the code below, I met an error and did not know why.

The code is:

program main
  implicit none
  real(8)::a(3,2),b(2,1)

  a=reshape((/1.0,2.0,3.0,4.0,5.0,6.0/),(/3,2/))
  b=reshape((/1.0,2.0/),(/2,1/))
  write(*,*)a(1,1:2)

  !Next sentence throw an error
  a(1,1:2)=transpose(b)
end program

The error is:

error #6366: The shapes of the array expressions do not conform. [A]

I think "a(1,1:2)" is one line and two columns, which is the same as "transpose(b)", why the compiler told me that "the shape do not conform"?

T X
  • 505
  • 1
  • 9
  • 19

1 Answers1

2

You are wrong, a(1,1:2) is not a 2D array (which you call a matrix), it is a 1D array.

Bu using a(1,.. you are selecting a definite "row" in the "matrix" from which you take a "row vector" 1:2).

You must use

a(1:1,1:2)

for a 2D array of shape 1x2 (matrix with one line and two columns if you want).