# Multiplying an array by the individual elements of a vector

**URL:** <https://discourse.julialang.org/t/multiplying-an-array-by-the-individual-elements-of-a-vector/36856>\
**Category:** New to Julia\
**Created:** [April 1, 2020, 3:05pm UTC](https://discourse.julialang.org/t/multiplying-an-array-by-the-individual-elements-of-a-vector/36856 "2020-04-01T15:05:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [April 1, 2020, 3:05pm UTC](https://discourse.julialang.org/t/multiplying-an-array-by-the-individual-elements-of-a-vector/36856/1 "2020-04-01T15:05:51Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 1, 2020, 3:15pm UTC](https://discourse.julialang.org/t/multiplying-an-array-by-the-individual-elements-of-a-vector/36856/2 "2020-04-01T15:15:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bcmichael](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcmichael/32/10003_2.png) [@bcmichael](https://discourse.julialang.org/u/bcmichael)\
**Post date:** [April 1, 2020, 3:16pm UTC](https://discourse.julialang.org/t/multiplying-an-array-by-the-individual-elements-of-a-vector/36856/3 "2020-04-01T15:16:01Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [April 1, 2020, 3:17pm UTC](https://discourse.julialang.org/t/multiplying-an-array-by-the-individual-elements-of-a-vector/36856/4 "2020-04-01T15:17:01Z")

</div>

Yep, thanks! I think that worked!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 1, 2020, 3:19pm UTC](https://discourse.julialang.org/t/multiplying-an-array-by-the-individual-elements-of-a-vector/36856/5 "2020-04-01T15:19:19Z")

</div>

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.
