Multiplying an array by the individual elements of a vector

So, I have an array, seed_dvh, and a vector, dvh_coeff, corresponding to some coefficients I’d like to multiply seed_dvh by. seed_dvh is of size 1718x2 and dvh_coeff is of length 1000.

I want to create a 1000 element vector of arrays, dvh_list, that consists of seed_dvh multiplied by the numbers in dvh_coeff
i.e. such that

dvh_list[1]=seed_dvh*dvh_coeff[1]
dvh_list[2]=seed_dvh*dvh_coeff[2]
dvh_list[3]=seed_dvh*dvh_coeff[3]

etc.

but I am not quite sure how to do this. I have tried dvh_list=dvh_coeff.*seed_dvh but of course this won’t work because the array sizes don’t match so pointwise multiplication is not possible.

Is there a way to do this without writing a for loop?
thanks.

Are you looking for dvh_list=[seed_dvh].*dvh_coeff?

2 Likes

dvh_list = [seed_dvh.*n for n in dvh_coeff]

It’s basically the same thing as writing a for loop, but compact and clear.

1 Like

Yep, thanks! I think that worked!

Note that the fully Julian way would be dvh_list=(seed_dvh,).*dvh_coeff or dvh_list=Ref(seed_dvh).*dvh_coeff as these make it easier for the compiler to avoid the array allocation. They do exactly the same thing though.

1 Like