6

I am trying to multiply part of a column vector (n,1) by a part of another row vector (1,n). Both parts have the same length. So I should get a matrix (n,n).

Here is my simple code:

PROGRAM test_pack_1
REAL :: m(1,10), x(10,1), y(10,10)

m = reshape( (/ 1, -1, 3, 2, 1, 2, -2, -2, 1, 0 /), (/ 1, 10 /))
x = reshape( (/ 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 /), (/ 10, 1 /))

y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))


DO j = 1,10
PRINT* ;WRITE(*,*) y(:,j)
ENDDO
print *

END PROGRAM

I'm Using:

ifort -g -debug -traceback -check all -ftrapuv test_cshift.f90

And I'm getting:

test_cshift.f90(7): error #6241: The shapes of the arguments are inconsistent or nonconformable.   [MATMUL]
y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))
-------------^
test_cshift.f90(7): error #6366: The shapes of the array expressions do not conform.   [Y]
y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
user3203131
  • 75
  • 1
  • 1
  • 4

1 Answers1

10

The problem is that x(1:9,1) isn't of shape [9 1] but [9]. You need to use x(1:9, 1:1). The same goes for m.

francescalus
  • 30,576
  • 16
  • 61
  • 96
  • Thanks. What about v*vT (two vectors that produce a matrix)? – Garini Aug 15 '17 at 15:51
  • One can't use `matmul` to multiply two rank-1 arrays (regardless of whether one represents a row vector and the other a column), but you can see related things in [this recent question](https://stackoverflow.com/q/44331867/3157076). – francescalus Aug 15 '17 at 16:52
  • Thank you, I did find a solution (which is also covered in the answers you pointed out). – Garini Aug 17 '17 at 15:30