 I’m trying to understand broadcasting better. Here is a simple task I was working on recently:

``````function basis(n,x)
return cos(n * pi * x)
end

x = rand(10)

samples = [basis.(n,x) for n=1:5]

``````

1- I was hoping to produce a 10 x 5 matrix, but it was an array of arrays???

2- Can I build the whole matrix with broadcasting?

The reason you got an `Array{Array}` is because `basis.(n,x)` creates an Array, and you put it inside an array comprehension. What you want is

``````x = rand(10)
n=(1:5)'
basis.(n,x)
``````
4 Likes

Whoa!! Cool. So is

``````
n = (1:5)'
``````

just a row vector? The prime just transposes the vector?

Technically it’s a adjoint (transpose + complex conjugate), but for real numbers, that’s just a transpose.

Other than that technicality, you are correct.

1 Like

Very cool. I’m guessing that the parentheses are only needed because of the adjoint. Can you always treat a UnitRange object as a column vector?

1 Like
``````julia> UnitRange <: AbstractVector
true
``````

yes

2 Likes

One small technicality is that `(1:5)'` is an `AbstractMatirx`. urrently there is no concept of row vector in Julia. Vectors are always columnar. If you transpose them you get a matrix with one row.

4 Likes