# Filling non-contiguous slices of an array with the real and imaginary part of another

**URL:** <https://discourse.julialang.org/t/filling-non-contiguous-slices-of-an-array-with-the-real-and-imaginary-part-of-another/54033>\
**Category:** Performance\
**Created:** [January 27, 2021, 5:46am UTC](https://discourse.julialang.org/t/filling-non-contiguous-slices-of-an-array-with-the-real-and-imaginary-part-of-another/54033 "2021-01-27T05:46:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [January 27, 2021, 5:46am UTC](https://discourse.julialang.org/t/filling-non-contiguous-slices-of-an-array-with-the-real-and-imaginary-part-of-another/54033/1 "2021-01-27T05:46:36Z")

</div>

Hello,

I would like to get your feedback on the fastest way to fill the 1 modulo 3 entries with the real part of an array and the 2 modul0 3 entries with the imaginary part of the same array.

```julia
using BenchmarkTools
A = rand(ComplexF64, 20, 50)

B = zeros(20,150)
B[:,1:3:end-1] .= real(A)
B[:,2:3:end] .= imag(A)

function test1(A)
B = zeros(20,150)
B[:,1:3:end-1] .= real(A)
B[:,2:3:end] .= imag(A)
end

@btime test1($A)

```

9.327 μs (4 allocations: 39.39 KiB)

---

<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:** [January 27, 2021, 6:14am UTC](https://discourse.julialang.org/t/filling-non-contiguous-slices-of-an-array-with-the-real-and-imaginary-part-of-another/54033/2 "2021-01-27T06:14:23Z")

</div>

Did you try dotting the `real` and `imag` calls, i.e.

> [@mleprovost](#):
>
> `B[:,2:3:end] .= imag.(A)`

---

<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:** [January 27, 2021, 6:14am UTC](https://discourse.julialang.org/t/filling-non-contiguous-slices-of-an-array-with-the-real-and-imaginary-part-of-another/54033/3 "2021-01-27T06:14:56Z")

</div>

Don’t forget to broadcast the real and imag functions to avoid allocations.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [January 27, 2021, 5:07pm UTC](https://discourse.julialang.org/t/filling-non-contiguous-slices-of-an-array-with-the-real-and-imaginary-part-of-another/54033/4 "2021-01-27T17:07:53Z")

</div>

Thank you for your answers. Just for future reference:

```julia
function test2(A)
B = zeros(20,150)
    B[:,1:3:end-1] .= real.(A)
    B[:,2:3:end] .= imag.(A)
end

@btime test2($A)

```

2.477 μs (2 allocations: 23.52 KiB)
