# Broadcasting like a Matrix

**URL:** <https://discourse.julialang.org/t/broadcasting-like-a-matrix/2767>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [March 20, 2017, 1:58am UTC](https://discourse.julialang.org/t/broadcasting-like-a-matrix/2767 "2017-03-20T01:58:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 1:58am UTC](https://discourse.julialang.org/t/broadcasting-like-a-matrix/2767/1 "2017-03-20T01:58:36Z")

</div>

I have a custom type that behaves like an `AbstractMatrix` but cannot be `<: AbstractMatrix`. Is it possible in 0.6 to have it broadcast like a matrix?

Right now I get an error:

```julia
julia> immutable MyMatrix end

julia> Base.size(::MyMatrix) = (3,3)

julia> Base.IndexStyle(::Type{MyMatrix}) = Base.IndexCartesian()

julia> Base.getindex(::MyMatrix,k::Int,j::Int) = 5

julia> A=zeros(3,3);

julia> A .= MyMatrix()
ERROR: MethodError: Cannot `convert` an object of type MyMatrix to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] setindex! at ./multidimensional.jl:247 [inlined]
 [2] macro expansion at ./broadcast.jl:152 [inlined]
 [3] macro expansion at ./simdloop.jl:72 [inlined]
 [4] macro expansion at ./broadcast.jl:0 [inlined]
 [5] _broadcast!(::Base.#identity, ::Array{Float64,2}, ::Tuple{Tuple{}}, ::Tuple{Tuple{}}, ::MyMatrix, ::Tuple{}, ::Type{Val{0}}, ::CartesianRange{CartesianIndex{2}}) at ./broadcast.jl:138
 [6] broadcast_c! at ./broadcast.jl:210 [inlined]
 [7] broadcast!(::Function, ::Array{Float64,2}, ::MyMatrix) at ./broadcast.jl:203

```

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 20, 2017, 2:09am UTC](https://discourse.julialang.org/t/broadcasting-like-a-matrix/2767/2 "2017-03-20T02:09:37Z")

</div>

Ah, I figured it out: you need to define a `containertype` and `getindex` for `CartesianIndex`:

```julia
julia> Base.getindex(A::MyMatrix,kj::CartesianIndex{2}) = A[kj[1],kj[2]]

julia> Base.Broadcast.containertype(::MyMatrix) = Array

julia> A .= MyMatrix()
3×3 Array{Float64,2}:
 5.0 5.0 5.0
 5.0 5.0 5.0
 5.0 5.0 5.0

```
