# Multiply the first column of a matrix by several different scalars

**URL:** https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897
**Category:** New to Julia
**Created:** [April 2, 2020, 10:28am UTC](https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897 "2020-04-02T10:28:40Z")
**Posts on this page:** 8
**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 2, 2020, 10:28am UTC](https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897/1 "2020-04-02T10:28:40Z")

</div>

I have a matrix, `A`, which is of size nx2 and a vector, `v`, of 1000 coefficients.  
I would like to create a vector of arrays, `A_mult` which has 1000 elements, such that every entry in A\_mult consists of A with the first column multiplied by a corresponding entry of v.  
i.e.

```julia
A_mult[1][:,1]=A[:,1]*v[1]
A_mult[1][:,2]=A[:,2]
A_mult[2][:,1]=A[:,1]*v[2]
A_mult[2][:,2]=A[:,2]

```

…etc

so essentially I want the first column of A to be multiplied by all the entries in v, and then store the resulting matrices in a new array. Is it possible to do this without a loop?

Thanks

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 2, 2020, 10:39am UTC](https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897/2 "2020-04-02T10:39:10Z")

</div>

Out of curiosity, why do you need to avoid a loop? Seems like it would be a bit cumbersome.

---

<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 2, 2020, 10:45am UTC](https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897/3 "2020-04-02T10:45:31Z")

</div>

I suppose I don’t, I was just under the impression that this might not be as efficient as compared to a vectorized form

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 2, 2020, 10:59am UTC](https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897/4 "2020-04-02T10:59:11Z")

</div>

No, that’s incorrect. Loops are normally the fastest way.

---

<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 2, 2020, 11:00am UTC](https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897/5 "2020-04-02T11:00:31Z")

</div>

Good to know! Loops it is then!

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 2, 2020, 11:03am UTC](https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897/6 "2020-04-02T11:03:47Z")

</div>

Matlab, R and Python, etc. have slow loops, while Julia has fast loops, like C or Fortran.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [April 2, 2020, 11:15am UTC](https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897/7 "2020-04-02T11:15:09Z")

</div>

I think you can just write `[A .* [s,1]' for s in v]`.

---

<div class="post-metadata">

### Author: ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)
#### Post date: [April 2, 2020, 1:14pm UTC](https://discourse.julialang.org/t/multiply-the-first-column-of-a-matrix-by-several-different-scalars/36897/8 "2020-04-02T13:14:03Z")

</div>

You should use a loop as other people have said, in Julia the loops are very quickly. Also, you could be interested also in [GitHub - JuliaSIMD/LoopVectorization.jl: Macro(s) for vectorizing loops.](https://github.com/chriselrod/LoopVectorization.jl.git).
