# How to take slice of array without it turning into vector

**URL:** <https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352>\
**Category:** New to Julia\
**Created:** [March 22, 2020, 5:22pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352 "2020-03-22T17:22:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kw\_martin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kw_martin/32/48868_2.png) [@kw\_martin](https://discourse.julialang.org/u/kw_martin)\
**Post date:** [March 22, 2020, 5:22pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/1 "2020-03-22T17:22:16Z")

</div>

Background, I am realizing a filter bank of FIR filters that involve matrix operations on Array{ComplexF64,2}. Every time I take a slice, it turns into an Array{ComplexF64,1} and I have to reshape to get it back into an n by m array before I can do further operations on it. Is there some way I can take a slice and have it remain as nx1, rather than a 1 dimension array or do I have do a reshape after taking each slice?

```julia
julia> xin = ones(ComplexF64,16,1);

julia> typeof(xin)
Array{Complex{Float64},2}

julia> xi = xin[1:4];

julia> typeof(xi)
Array{Complex{Float64},1}

julia> xi = reshape(xi,4,1);

julia> typeof(xi)
Array{Complex{Float64},2}

julia> 

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 22, 2020, 5:26pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/2 "2020-03-22T17:26:01Z")

</div>

Slice in both dimensions.

```julia
xi = xin[1:4, :];

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [March 22, 2020, 5:38pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/3 "2020-03-22T17:38:57Z")

</div>

`xin[1:4,1:1]` is probably what you want

---

<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:** [March 22, 2020, 5:57pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/4 "2020-03-22T17:57:23Z")

</div>

Could you explain why you need it to remain as a 2d array? Can’t it just be a 1d vector?

---

<div class="post-metadata">

**Author:** ![kw\_martin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kw_martin/32/48868_2.png) [@kw\_martin](https://discourse.julialang.org/u/kw_martin)\
**Post date:** [March 22, 2020, 8:05pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/5 "2020-03-22T20:05:43Z")

</div>

DNF, for example:

```julia
julia> a = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
4×4 Array{Int64,2}:
  1 2 3 4
  5 6 7 8
  9 10 11 12
 13 14 15 16
julia> b = [1 1; 1 1; 1 1; 1 1]
4×2 Array{Int64,2}:
 1 1
 1 1
 1 1
 1 1
julia> c = a[2,:]*b
ERROR: DimensionMismatch("matrix A has dimensions (4,1), matrix B has dimensions (4,2)")
julia> c = reshape(a[2,:],1,4)*b
1×2 Array{Int64,2}:
 26 26

```

Gunnar, a[1:4, :] just gives a back, not a 1x4 array

```julia
julia> a[1:4, :]
4×4 Array{Int64,2}:
  1 2 3 4
  5 6 7 8
  9 10 11 12
 11 12 13 14

```

jishnub, I think your suggestion solves my issue

```julia
julia> c = a[2:2,:]*b
1×2 Array{Int64,2}:
 26 26
julia> size(a[2,:])
(4,)
julia> size(a[2:2,:])
(1, 4)

```

I would never have figured this out on my own. Thank you.

---

<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:** [March 22, 2020, 8:24pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/6 "2020-03-22T20:24:08Z")

</div>

> [@kw\_martin](#):
>
> ```julia
> julia> c = a[2,:]*b 
> ERROR: DimensionMismatch("matrix A has dimensions (4,1), matrix B has dimensions (4,2)") 
> julia> c = reshape(a[2,:],1,4)*b 
> 1×2 Array{Int64,2}:
> 26 26
> 
> ```

What about

```julia
julia> c = a[2,:]' * b
1×2 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:
 26 26

```

?

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [March 22, 2020, 8:27pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/7 "2020-03-22T20:27:15Z")

</div>

> [@DNF](#):
>
> julia\> c = a[2,:]’ \* b 1×2 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}: 26 26

This is much more readable and you are not messing around with your structure too much.

```julia
julia> permutedims(a[2,:]) * b
1×2 Array{Int64,2}:
 26 26

```

---

<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:** [March 22, 2020, 8:28pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/8 "2020-03-22T20:28:20Z")

</div>

How so? To me

```julia
c = a[2,:]' * b

```

seems much more like something straight out of a maths book, and _more_ readable.

Also:

```julia
julia> @btime ($a)[2,:]' * $b;
  99.752 ns (3 allocations: 224 bytes)

julia> @btime permutedims(($a)[2,:]) * $b
  291.106 ns (8 allocations: 528 bytes)

```

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [March 22, 2020, 8:31pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/9 "2020-03-22T20:31:09Z")

</div>

Well, in larger code, spotting a `'` is harder than explicitly stating it. The timing difference is negligible tbh.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 22, 2020, 8:32pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/10 "2020-03-22T20:32:10Z")

</div>

Only one of them is correct for complex matrices though, which is what’s discussed here.

---

<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:** [March 22, 2020, 8:32pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/11 "2020-03-22T20:32:31Z")

</div>

OK, that’s more a matter of taste, but in that case, use `transpose`, which doesn’t allocate.

---

<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:** [March 22, 2020, 8:32pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/12 "2020-03-22T20:32:53Z")

</div>

No, this was about real valued matrices.

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [March 22, 2020, 8:34pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/13 "2020-03-22T20:34:24Z")

</div>

Yes, either `transpose!` or `permutedims!` work. Comes down to preference 🙂

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 22, 2020, 8:35pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/14 "2020-03-22T20:35:34Z")

</div>

> [@DNF](#):
>
> No, this was about real valued matrices.

The original post was very clearly talking about Array{ComplexF64,2}.

---

<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:** [March 22, 2020, 8:37pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/15 "2020-03-22T20:37:03Z")

</div>

Anyway, my point was to question the need to stick to matrices when slicing. You can just as well accept vectors and then use `'` or `transpose`.

---

<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:** [March 22, 2020, 8:37pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/16 "2020-03-22T20:37:58Z")

</div>

> [@GunnarFarneback](#):
>
> The original post was very clearly talking about Array{ComplexF64,2}.

OK, but I was replying to a specific example. And switching to `transpose` doesn’t change the main point.

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [March 22, 2020, 8:39pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/17 "2020-03-22T20:39:50Z")

</div>

Sure, that’s a vaild point. I am also note sure why a matrix is really needed. A vector should work just fine and would be easier to debug imo if something goes wrong later (assuming this is part of some larger code).

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 22, 2020, 8:50pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/18 "2020-03-22T20:50:00Z")

</div>

The examples have been a bit contradictory but I suspect that either it’s not really necessary to extract slices _or_ things would become easier by transposing the entire problem so the filters are columns instead of rows.

---

<div class="post-metadata">

**Author:** ![kw\_martin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kw_martin/32/48868_2.png) [@kw\_martin](https://discourse.julialang.org/u/kw_martin)\
**Post date:** [March 23, 2020, 1:55am UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/19 "2020-03-23T01:55:33Z")

</div>

DNF, a[2,:]'\*b is fine with real matrices, but with complex matrices (which is pretty well everything I use), one would need transpose(a[2,:])\*b

---

<div class="post-metadata">

**Author:** ![kw\_martin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kw_martin/32/48868_2.png) [@kw\_martin](https://discourse.julialang.org/u/kw_martin)\
**Post date:** [March 27, 2020, 1:57pm UTC](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352/20 "2020-03-27T13:57:27Z")

</div>

Gunnar, my understanding is it’s better to use columns for the variable that iterates in the inside loop. For filters, I tend to reserve this for containers that have elements changing with time. Hence, the use of rows for each stage of a cascade filter. I should also mention again that all the filters are complex; for the special case of real inputs, they can be simplified to real filters, but this often adds complexity as then often one must then guarantee all poles occur in complex conjugate pairs, “clean” them when they are not exactly complex conjugate etc. So unless I need the time and or/space optimizations, I generally leave the filters as complex, at least when first developing them for a particular application. Also, I am working on filter banks, so if I want them to be usable for phased-array applications, then again I prefer to have them as complex, at least for now. My personable opinion (not shared by others as far as I can tell), is that complex signal processing is actually cleaner and simpler than “real” signal processing.

[Next page](https://discourse.julialang.org/t/how-to-take-slice-of-array-without-it-turning-into-vector/36352.md?page=2)
