# Mold matrix of the same size

**URL:** <https://discourse.julialang.org/t/mold-matrix-of-the-same-size/2008>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 9, 2017, 9:56pm UTC](https://discourse.julialang.org/t/mold-matrix-of-the-same-size/2008 "2017-02-09T21:56:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [February 9, 2017, 9:56pm UTC](https://discourse.julialang.org/t/mold-matrix-of-the-same-size/2008/1 "2017-02-09T21:56:07Z")

</div>

Hi,  
I want to define a matrix with the size of the existing one:

```julia
sz = size(A);
Id = eye(sz);

```

This produces an error:

> ERROR: MethodError: no method matching eye(::Tuple{Int64,Int64})

How do I create a matrix of the same fast, without element-wise access to the tuple?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 9, 2017, 10:09pm UTC](https://discourse.julialang.org/t/mold-matrix-of-the-same-size/2008/2 "2017-02-09T22:09:56Z")

</div>

If your array `A` has one or two dimensions, then you can do:

```julia
eye(sz...)

```

which, if `sz` is of length 2, is equivalent to:

```julia
eye(sz[1], sz[2])

```

This is called “splatting”: [http://docs.julialang.org/en/release-0.5/manual/faq/#splits-one-argument-into-many-different-arguments-in-function-calls](http://docs.julialang.org/en/release-0.5/manual/faq/#splits-one-argument-into-many-different-arguments-in-function-calls)

edit: @DNF has an even better suggestion below.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 9, 2017, 10:21pm UTC](https://discourse.julialang.org/t/mold-matrix-of-the-same-size/2008/3 "2017-02-09T22:21:04Z")

</div>

You can just do

```julia
Id = eye(A)

```

and then you get `Id` with the same shape and element type as `A`.

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [February 9, 2017, 11:25pm UTC](https://discourse.julialang.org/t/mold-matrix-of-the-same-size/2008/4 "2017-02-09T23:25:58Z")

</div>

@DNF @rdeits  
Is there a fast way to double the dimensions, smth like:  
`eye(2*size(a))`  
to create a matrix of (2n,2m) size?

---

<div class="post-metadata">

**Author:** ![Muninn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muninn/32/361_2.png) [@Muninn](https://discourse.julialang.org/u/Muninn)\
**Post date:** [February 10, 2017, 1:40am UTC](https://discourse.julialang.org/t/mold-matrix-of-the-same-size/2008/5 "2017-02-10T01:40:42Z")

</div>

Interestingly it seems `*` doesn’t broadcast properly along tuples in 0.5 (I seem to remember reading that in 0.5 the `.*` syntax is actually a special function and not actually broadcasting `*` but I may be wrong), but likely in 0.6 you could do `2.*size(a)` to get a tuple `(2n, 2m)` like you’d expect.

In 0.5 you could do `2.*collect(size(a))` although it’s far from elegant. If this is something you want to do a lot I’d likely make some function like

```julia
function scaled_eye(a, N)
    return eye(N.*collect(size(a))...)
end

```

and then you’d be able to do `scaled_eye(a, 2)` to get the result you want without worrying about the correct operations you need to multiple a tuple and splat it

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 10, 2017, 8:15am UTC](https://discourse.julialang.org/t/mold-matrix-of-the-same-size/2008/6 "2017-02-10T08:15:42Z")

</div>

This works too:

```julia
eye(2.*[size(A)...]...)

```

though it’s not exactly elegant, either.

It’s slightly surprising that `eye` doesn’t accept tuples, while `zeros` and `ones` do, but it might be related to the fact that `eye` can only accept at most two size parameters, while the others can take an arbitrary number of dimensions.

In many cases, though, you are better off using the `UniformScaling` operator `I` instead of `eye`. See: [http://docs.julialang.org/en/stable/manual/linear-algebra/#the-uniform-scaling-operator](http://docs.julialang.org/en/stable/manual/linear-algebra/#the-uniform-scaling-operator)

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [February 10, 2017, 12:25pm UTC](https://discourse.julialang.org/t/mold-matrix-of-the-same-size/2008/7 "2017-02-10T12:25:30Z")

</div>

Unfortunately, `I` cannot be a block in a matrix.

The problem is not that eye() doesn’t accept tuples, but that I cannot multiply tuple by scalar, that BTW seems to be reasonable.
