Unexpected broadcasting behaviour (?)

Hi
I was wondering if the MWE below yields the expected behaviour.
Why is the result not symmetric?
I would have expected symmetric resulting arrays sizes from this.
Cheers!

let 
    b = ones(3, 4)
    a = ones(1, 2)
    x = a .* b[2,2:end-1]
    @show size(x)

    b = ones(4, 3)
    a = ones(2, 1)
    x = a .* b[2:end-1,2]
    @show size(x)
end

You can make sense of it by always considering Vectors to be column vectors, i.e. n \times 1 matrices.

julia> size(rand(1, 2) .* rand(2, 1))  # broadcast to 2x2
(2, 2)

julia> size(rand(1, 2) .* rand(2))  # same
(2, 2)

julia> size(rand(2, 1) .* rand(1, 2))  # broadcast to 2x2
(2, 2)

julia> size(rand(2, 1) .* rand(2))  # same as 2 x 1 .* 2 x 1  
(2, 1)

julia> size(rand(2, 1) .* rand(2, 1))
(2, 1)

Is it because what you want is:

let 
    b = ones(3, 4)
    a = ones(1, 2)
    x = a .* b[2:2,2:end-1]
    @show size(x)

    b = ones(4, 3)
    a = ones(2, 1)
    x = a .* b[2:end-1,2:2]
    @show size(x)
end

If an index is an integer rather than a range, then a Matrix becomes a Vector

3 Likes