# Assigning a UnitRange to an array

**URL:** https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115
**Category:** General Usage
**Created:** [February 24, 2023, 5:53am UTC](https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115 "2023-02-24T05:53:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sransome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sransome/32/29810_2.png) [@sransome](https://discourse.julialang.org/u/sransome)
#### Post date: [February 24, 2023, 5:53am UTC](https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115/1 "2023-02-24T05:53:14Z")

</div>

Hi,

I have a matrix with elements of type UnitRange

```julia
a = Array{UnitRange}(undef ,2,2)

```

I would like to assign a unit range like (200:375) to a range of elements using broadcast.

```julia
a[1,:] .= (200:375)

```

but I get this error

```julia
ERROR: DimensionMismatch: array could not be broadcast to match destination

```

Clearly Julia wants to interpret the UnitRange rather than just assign the array elements to the UnitRange.

Is there a way to achieve what I need?

Thanks  
Steve

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [February 24, 2023, 5:55am UTC](https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115/2 "2023-02-24T05:55:56Z")

</div>

Have you tried this?

```julia
a[1,:] .= Ref(200:375)

```

---

<div class="post-metadata">

### Author: ![sransome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sransome/32/29810_2.png) [@sransome](https://discourse.julialang.org/u/sransome)
#### Post date: [February 24, 2023, 5:56am UTC](https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115/3 "2023-02-24T05:56:53Z")

</div>

Perfect, thanks.

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [February 24, 2023, 5:59am UTC](https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115/4 "2023-02-24T05:59:21Z")

</div>

Also, to maintain type stability, you might consider setting `a`’s element type to a _concrete type_. `UnitRange` is a `UnionAll`, and to specify a concrete type you need to parameterize it with the type of its elements—in this case, `UnitRange{Int}`:

```julia
a = Array{UnitRange{Int}}(undef, 2, 2)

```

---

<div class="post-metadata">

### Author: ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)
#### Post date: [February 24, 2023, 6:52am UTC](https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115/5 "2023-02-24T06:52:13Z")

</div>

> [@sransome](#):
>
> `a = Array{UnitRange}(undef ,2,2)`

The reason it won’t broadcast is because the range has a length and so will try to broadcast over that length. The `Ref` works because this has a length one.

As an alternative to the `Ref` which may in some situations be allocating, you can also wrap it as a 1-tuple or in the StaticArrays `Scalar` made specifically for this reason:

```julia
# Tuple
a[1, :] .= (200:375,)

# Scalar
using StaticArrays
a[1, :] .= Scalar(200:375)

```

I prefer `Scalar` as this is more semantic - the next person looking at your code doesn’t have to work out why you used a Ref or a tuple, the reason is right there in the documentation for Scalar.

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [February 24, 2023, 7:44am UTC](https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115/6 "2023-02-24T07:44:25Z")

</div>

Huh, didn’t know about `Scalar`. Definitely feels like something that should be in `Base`; the `Ref` trick to cause scalar broadcasting always felt like an ugly hack, especially since you aren’t using it for its mutability.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 24, 2023, 10:36am UTC](https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115/7 "2023-02-24T10:36:22Z")

</div>

Just to add maybe part of your confusion stems from the fact that the brackets arround the range don’t do anything here, the key is this difference:

```julia
julia> (200:375)
200:375

julia> (200:375,)
(200:375,)

```

this is a difference between arrays and tuples:

```julia
julia> a[1,:] .= [200:375]
2-element view(::Matrix{UnitRange}, 1, :) with eltype UnitRange:
 200:375
 200:375

```

for tuples just doing `(x)` isn’t possible because parens are used to group expressions as well, while `[x]` will create a 1-element vector. You normally don’t want to use `[x]` though because that has to allocate, hence why people go for `(x,)`, `Ref`, or `Scalar` as discussed.

---

<div class="post-metadata">

### Author: ![sransome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sransome/32/29810_2.png) [@sransome](https://discourse.julialang.org/u/sransome)
#### Post date: [February 25, 2023, 3:17am UTC](https://discourse.julialang.org/t/assigning-a-unitrange-to-an-array/95115/8 "2023-02-25T03:17:11Z")

</div>

Thanks for all the help here. Scalar looks like the best solution for me.
