Q: Defining a real scalar equal to an 1x1 matrix Let A and B be matrices of size 1 times n and n times 1, respectively.
Then the multiplication of A with B is a 1 times 1 matrix.
Which is the better way to assign the value of MATMUL(A,B) to a real number x?
I would like to write:
x=MATMUL(A,B)  ! <<--- but this is wrong.

The above expression is wrong because I'm trying to assign a 1 times 1 matrix to a real number.
My solution is to define a 1 times 1 matrix C and with this:
C=MATMUL(A,B)
x=C(1,1)         ! <--- this solution is ok, but is too long

But, there exists a better way to assign MATMUL(A,B) to the real number x?
The entire code with my question is as follow:
PROGRAM testing
  !
  IMPLICIT NONE
  REAL :: A(1,2),B(2,1),C(1,1),x
  !
  A(1,1)=1.0; A(1,2)=3.5
  B(1,1)=2.0; B(2,1)=5.0
  C=MATMUL(A,B)               ! it is ok
  x=MATMUL(A,B)               ! it is wrong
  x=C(1,1)                    ! it is ok     <--- exists a better way ??
  !
END PROGRAM testing


A: You have noticed that it is not possible to do intrinsic assignment of an array to a scalar (and C is a rank-2 array of size 1).  x=C(1,1) is the correct way to do such assignment from the single element of C to the scalar x.
There are other ways to abstract that correct assignment statement, but probably little value in doing so.
In your specific case, however, there is alternative.  Rather than matmul, consider dot_product.
x = DOT_PRODUCT(A(1,:), B(:,1))  ! Scalar result, intrinsic assignment allowed.


A: per my comment, you can write a very simple function to extract the first element of an array:
real function first(matrix) !return the (1,1,1,..) element of an array
real, intent(in) :: matrix(*)
first=matrix(1)
end function

simply use as:
real :: a(1,2),b(2,1),x
...
x=first(matmul(a,b))

note if you want to make sure this is only used for a dimension(1,1) array you need to use an explicit interface and do:
real function first(matrix)
real, intent(in) :: matrix(:,:)
if(.not.all(shape(matrix).eq.[1,1]))reporterror()
first=matrix(1,1)
end function

