Fortran element wise multiplication of a matrix and a vector
问题 Is there a simple and quick way to multiply a column of a matrix with element of a vector. We can do this explicitly, program test integer :: x(3,3), y(3), z(3,3) x = reshape([(i,i=1,9)],[3,3]) y = [1,2,3] do i=1,3 z(:,i) = x(:,i) * y(i) print *, z(:,i) enddo end program test Is there a way to perform the do loop in one line. For example in Numpy python we can do this to do the job in one shot z = np.einsum('ij,i->ij',x,y) #or z = x*y[:,None] 回答1: Try z = x * spread(y,1,3) and if that doesn't