# One element vector cannot be used to multiply another vector/matrix

**URL:** <https://discourse.julialang.org/t/one-element-vector-cannot-be-used-to-multiply-another-vector-matrix/71256>\
**Category:** General Usage\
**Tags:** linearalgebra, arrays\
**Created:** [November 10, 2021, 1:03pm UTC](https://discourse.julialang.org/t/one-element-vector-cannot-be-used-to-multiply-another-vector-matrix/71256 "2021-11-10T13:03:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [November 10, 2021, 1:03pm UTC](https://discourse.julialang.org/t/one-element-vector-cannot-be-used-to-multiply-another-vector-matrix/71256/1 "2021-11-10T13:03:59Z")

</div>

`rand(1,3)*rand(3)`  
is possible, and returns a 1-element vector  
but `rand(1,3)*rand(3)*(another matrix)` is usually not possible,  
`rand(1,3)*rand(3)*rand(3)` gives error:  
`MethodError: no method matching *(::Vector{Float64}, ::Vector{Float64})`

I understand Julia is a strongly typed language (hope it means what I want to say). So bascially i understand why it’s not possible in Julia. But this is funny when encountered

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 10, 2021, 1:09pm UTC](https://discourse.julialang.org/t/one-element-vector-cannot-be-used-to-multiply-another-vector-matrix/71256/2 "2021-11-10T13:09:16Z")

</div>

Julia follows `*` matrix-multiplication rules, i.e. the matrices/vectors/tensors need to have correct shapes for that to work. Alternatively, you can use the `.*` to do element-wise multiplication (with potential broadcasting); again the operands need to have sizes which fit under element-wise rules. Have a look at the manual, that should be fairly complete on the subject. Your example “works” with element-wise multiplcation

```julia
julia> rand(1,3).*rand(3).*rand(3)
3×3 Matrix{Float64}:
 0.0771117 0.044531 0.199846
 0.0370739 0.0214097 0.0960824
 0.141504 0.0817167 0.366728

```

---

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [November 10, 2021, 1:10pm UTC](https://discourse.julialang.org/t/one-element-vector-cannot-be-used-to-multiply-another-vector-matrix/71256/3 "2021-11-10T13:10:48Z")

</div>

I understand your point. But what i want is actually something like  
`(rand(1,3)*rand(3)) .*rand(4)`

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 10, 2021, 1:20pm UTC](https://discourse.julialang.org/t/one-element-vector-cannot-be-used-to-multiply-another-vector-matrix/71256/4 "2021-11-10T13:20:37Z")

</div>

> [@Fred\_He](#):
>
> (rand(1,3)\*rand(3)) .\*rand(4)

that works 🙂

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 10, 2021, 1:30pm UTC](https://discourse.julialang.org/t/one-element-vector-cannot-be-used-to-multiply-another-vector-matrix/71256/5 "2021-11-10T13:30:47Z")

</div>

I agree it’s a noteworthy observation! It can be surprising coming from Matlab: Matlab also follows matrix multiplication rules and operands must have the right shape so `[1; 2; 3] * [1; 2; 3]` doesn’t work. However the following does work in Matlab:

```matlab
a = rand(3, 1);
b = rand(3, 1);
c = rand(3, 1);

a' * b * c

```

Matlab doesn’t distinguish between scalars and 1x1 matrices, so the above can be interpreted as a scalar `a'*b` multiplying `c`. But as you say it doesn’t work in Julia because `a'*b` is not a scalar, it’s a 1-element matrix.

Another way to put it: in Matlab you can implement `dot(a, b)` as `a' * b`. In Julia this is not a correct implementation of the dot product (Edit: in the sense that it fails if `a` or `b` is a nx1 matrix rather than a vector).

Note that you can also write this calculation as `c * a' * b`. This works both in Julia and Matlab, but I fear it will do the inefficient `(c * a') * b` rather than the efficient `c * (a' * b)`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 10, 2021, 2:10pm UTC](https://discourse.julialang.org/t/one-element-vector-cannot-be-used-to-multiply-another-vector-matrix/71256/6 "2021-11-10T14:10:50Z")

</div>

Not sure if that is clear, but if all where actually vectors all that works, i. e:

```julia
julia> rand(3)' * rand(3) # is a dot product, returns a scalar
0.3042989054310262

```

all the confusion boils down to the difference between matrix with one column and a vector, difference that does not exist in Matlab.

That is, in Julia `rand(3,1)` is not the same as `rand(3)`, and unless you get that 1-column matrix from another source, that is not the Julian way to define vectors. You can also convert that one-column matrix to a vector, using `vec(rand(3,1))`.

---

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [November 11, 2021, 12:48pm UTC](https://discourse.julialang.org/t/one-element-vector-cannot-be-used-to-multiply-another-vector-matrix/71256/7 "2021-11-11T12:48:43Z")

</div>

I think this hits the true reason.
