Use of squeeze function

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"

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

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

can somebody help me to figure this out?

Does the documentation help you?

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?

1 Like

Indexing like that already squeezes the array for you automatically.

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> r[:,2:2,:]
2×1×2 Array{Float64,3}:
[:, :, 1] =
 0.0481443
 0.449423

[:, :, 2] =
 0.794598
 0.798384
2 Likes

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

1 Like