I have an array A of size M,N,P, and a vector v of size P. And I want to multiply each “plane” A[:,:,i] of the array by v[i]. I can do this in a loop, or with two calls to reshape. But is there any way of doing it directly?

If the “P” dimension was first; that is, if the array had size P,M,N, then I could:

v' .* A

I’m just wondering if there’s something similar for when the “P” dimension is last; that is, a scalar multiplication in which you can choose the dimension of the array to be used, Thanks!

Broadcasting is good for this, and you only need one reshape to put v along the 3rd dimension. I made a package which keeps track of these reshapes & permutations for me, instead of writing comments explaining which dimension was 3rd, or 4th, and wondering if it should be (2,3,1) or (3,1,2):

using TensorCast
@cast B[m,n,i] := A[m,n,i] * v[i] # A .* reshape(v, 1,1,:)
@cast B[i,m,n] := A[i,m,n] * v[i] # A .* v
@cast B[i,m,n] := A[m,n,i] * v[i] # PermutedDimsArray(A, (3,1,2)) .* v

Thanks, everyone. It seems that once I get into multidimensional arrays I fall into the world of tensors, in which as you say something like einsum.jl may do the job. Or write a loop myself… or re-write my code so that my “dimension of interest” (as it were) is first. This is what I’m currently doing as it is simple, requires no extra packages or macros, and seems to work fine. But it’s also somewhat inflexible, so I will indeed explore the other options! Again, many thanks.