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

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.

``````A_mult[:,1]=A[:,1]*v
A_mult[:,2]=A[:,2]
A_mult[:,1]=A[:,1]*v
A_mult[:,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

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

1 Like

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

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

2 Likes

Good to know! Loops it is then!

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

2 Likes

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

You should use a loop as other people have said, in Julia the loops are very quickly. Also, you could be interested also in https://github.com/chriselrod/LoopVectorization.jl.git.