# Use of squeeze function

**URL:** https://discourse.julialang.org/t/use-of-squeeze-function/9500
**Category:** General Usage
**Created:** [March 5, 2018, 8:40am UTC](https://discourse.julialang.org/t/use-of-squeeze-function/9500 "2018-03-05T08:40:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Samudra](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Samudra](https://discourse.julialang.org/u/Samudra)
#### Post date: [March 5, 2018, 8:40am UTC](https://discourse.julialang.org/t/use-of-squeeze-function/9500/1 "2018-03-05T08:40:07Z")

</div>

Hi ,  
I am trying to understand the way how squeeze function works,  
when I tried to do the following it gives me the error " squeezed dims must all be size 1"

```julia
P = 0.1*ones(Float64, 4, 9)

for i=2:4
  squeeze(P[i,:],1)
end

```

can somebody help me to figure this out?

---

<div class="post-metadata">

### Author: ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)
#### Post date: [March 5, 2018, 8:43am UTC](https://discourse.julialang.org/t/use-of-squeeze-function/9500/2 "2018-03-05T08:43:19Z")

</div>

Does the documentation help you?

```julia
squeeze(A, dims)

  Remove the dimensions specified by dims from array A. Elements of dims must
  be unique and within the range 1:ndims(A). size(A,i) must equal 1 for all i
  in dims.

     Example
    ≡≡≡≡≡≡≡≡≡

  julia> a = reshape(collect(1:4),(2,2,1,1))
  2×2×1×1 Array{Int64,4}:
  [:, :, 1, 1] =
   1 3
   2 4
  
  julia> squeeze(a,3)
  2×2×1 Array{Int64,3}:
  [:, :, 1] =
   1 3
   2 4

```

In your case `P[i,:]` already returns a `Vector` what do you want to `squeeze` there?

---

<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 5, 2018, 2:05pm UTC](https://discourse.julialang.org/t/use-of-squeeze-function/9500/3 "2018-03-05T14:05:54Z")

</div>

Indexing like that already squeezes the array for you automatically.

```julia
julia> r = rand(2,3,2)
2×3×2 Array{Float64,3}:
[:, :, 1] =
 0.292798 0.0481443 0.487387
 0.563843 0.449423 0.586495

[:, :, 2] =
 0.169698 0.794598 0.823726 
 0.587734 0.798384 0.0610884

julia> r[:, 2, :]
2×2 Array{Float64,2}:
 0.0481443 0.794598
 0.449423 0.798384

```

Squeezing again after that will therefore fail.

If you _don’t_ want it to squeeze, you have to index with a range:

```julia
julia> r[:,2:2,:]
2×1×2 Array{Float64,3}:
[:, :, 1] =
 0.0481443
 0.449423

[:, :, 2] =
 0.794598
 0.798384

```

---

<div class="post-metadata">

### Author: ![Samudra](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Samudra](https://discourse.julialang.org/u/Samudra)
#### Post date: [March 6, 2018, 6:33am UTC](https://discourse.julialang.org/t/use-of-squeeze-function/9500/4 "2018-03-06T06:33:43Z")

</div>

yeah now I get it, Thanks Guys, still a beginner to Julia 🙂
