# Assign Uniform scaling to Matrix with setindex

**URL:** <https://discourse.julialang.org/t/assign-uniform-scaling-to-matrix-with-setindex/9919>\
**Category:** General Usage\
**Created:** [March 23, 2018, 12:02pm UTC](https://discourse.julialang.org/t/assign-uniform-scaling-to-matrix-with-setindex/9919 "2018-03-23T12:02:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 23, 2018, 12:02pm UTC](https://discourse.julialang.org/t/assign-uniform-scaling-to-matrix-with-setindex/9919/1 "2018-03-23T12:02:27Z")

</div>

Say I have a function

```julia
function f(a,x)
    a[:,:] = x
end
a = zeros(2,2)
x = eye(2)
f(a,x) # Works fine
f(a,I) # Does not work
ERROR: MethodError: Cannot `convert` an object of type LinearAlgebra.UniformScaling{Bool} to an object of type Float64

```

Is there something I can do in `f` such that it can handle both input arguments of type `X::Matrix` and `x::UniformScaling`? My example `f` is simple, but my real use case involves a more complicated function and it would be nice if it could handle arguments of type `UniformScaling`  
Thanks!

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 23, 2018, 12:08pm UTC](https://discourse.julialang.org/t/assign-uniform-scaling-to-matrix-with-setindex/9919/2 "2018-03-23T12:08:25Z")

</div>

The following works, but is arguably not that pretty

```julia
function f(a,x)
    a[:,:] = x isa AbstractMatrix ? x : Matrix(x)
end

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 23, 2018, 12:16pm UTC](https://discourse.julialang.org/t/assign-uniform-scaling-to-matrix-with-setindex/9919/3 "2018-03-23T12:16:52Z")

</div>

`copy!(a, x)` (or `Compat.copyto!(a, x)` to be 0.7-compliant) works.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 23, 2018, 12:22pm UTC](https://discourse.julialang.org/t/assign-uniform-scaling-to-matrix-with-setindex/9919/4 "2018-03-23T12:22:43Z")

</div>

Thanks! I ended up having to use

```julia
copy!(@view(a[:,:,1]), x)

```

since my actual target matrix was three dimensional.
