# Assigning array to a slice of another array

**URL:** <https://discourse.julialang.org/t/assigning-array-to-a-slice-of-another-array/88284>\
**Category:** New to Julia\
**Tags:** arrays\
**Created:** [October 5, 2022, 12:15pm UTC](https://discourse.julialang.org/t/assigning-array-to-a-slice-of-another-array/88284 "2022-10-05T12:15:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SiddhantDeshmukh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siddhantdeshmukh/32/35324_2.png) [@SiddhantDeshmukh](https://discourse.julialang.org/u/SiddhantDeshmukh)\
**Post date:** [October 5, 2022, 12:15pm UTC](https://discourse.julialang.org/t/assigning-array-to-a-slice-of-another-array/88284/1 "2022-10-05T12:15:34Z")

</div>

Hi, I’ve been trying to vectorize the following operation

```julia
for i = 1:N
    arr[i, :, :] .= arr[a, :, :]
end

```

over the indices ‘i’ as

```julia
@. arr[b:c, :, :] = arr[a, :, :]

```

(where ‘b’ = 1, ‘c’ = N)  
such that all of the elements b:c are copied from index ‘a’. However, this throws an error:  
DimensionMismatch(“array could not be broadcast to match destination”)

Makes sense, since I’m trying to assign, e.g. a 5x5 array (RHS) to a 2x5x5 array (LHS). What I want is for all the elements sliced on the LHS array to be the elements copied from arr[a, :, :]. Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [October 5, 2022, 12:30pm UTC](https://discourse.julialang.org/t/assigning-array-to-a-slice-of-another-array/88284/2 "2022-10-05T12:30:04Z")

</div>

`arr[b:c, :, :] .= arr[a:a,:, :]` should work.  
Note `arr[a:a,:,:]` is a 1x5x5 array and not a 5x5 array like `arr[a,:,:]`. This is important to let broadcasting `.=` know which dimension to replicate on.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 5, 2022, 3:01pm UTC](https://discourse.julialang.org/t/assigning-array-to-a-slice-of-another-array/88284/3 "2022-10-05T15:01:00Z")

</div>

> [@SiddhantDeshmukh](#):
>
> I’ve been trying to vectorize the following operation

As a side note, in case you’re doing this for performance reasons: Loops and general non-vectorized code in Julia have no inherent penalty to them, and can be as fast as (or faster than) vectorized code. So your `for` loop can stand as-is unless you have some other reason to change 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:** [October 5, 2022, 3:12pm UTC](https://discourse.julialang.org/t/assigning-array-to-a-slice-of-another-array/88284/4 "2022-10-05T15:12:44Z")

</div>

Another thing is that Julia `Array`s are column major, unlike in numpy/C++/etc. So it would be (possibly much) faster to arrange your data so you can do

```julia
arr[:, :, i] .= arr[:, :, a]

```

In general, it’s a good idea to take this into account when designing your data layout.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 5, 2022, 9:51pm UTC](https://discourse.julialang.org/t/assigning-array-to-a-slice-of-another-array/88284/5 "2022-10-05T21:51:21Z")

</div>

> [@SiddhantDeshmukh](#):
>
> `arr[i, :, :] .= arr[a, :, :]`

For better performance, you may consider using views:

```julia
arr[i, :, :] .= view(arr, a, :, :)

```

---

<div class="post-metadata">

**Author:** ![SiddhantDeshmukh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siddhantdeshmukh/32/35324_2.png) [@SiddhantDeshmukh](https://discourse.julialang.org/u/SiddhantDeshmukh)\
**Post date:** [October 6, 2022, 8:17am UTC](https://discourse.julialang.org/t/assigning-array-to-a-slice-of-another-array/88284/6 "2022-10-06T08:17:52Z")

</div>

Thanks for the tip, I’ll see if I can do so!

---

<div class="post-metadata">

**Author:** ![SiddhantDeshmukh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siddhantdeshmukh/32/35324_2.png) [@SiddhantDeshmukh](https://discourse.julialang.org/u/SiddhantDeshmukh)\
**Post date:** [October 6, 2022, 8:19am UTC](https://discourse.julialang.org/t/assigning-array-to-a-slice-of-another-array/88284/7 "2022-10-06T08:19:24Z")

</div>

Thanks, I’ve added the @views macro to the relevant lines.
