MATLAB: What does squeeze() do in MATLAB

squeeze

Hi All,
Can anyone tell me about the use of squeeze() function in a MATLAB program?
Thanks in advance!
Swati

Best Answer

Squeeze is a useful tool to help compress dimensions of a multidimensional array when you have performed some operations. Let me give you a couple of examples as to why it is useful.
Suppose we have a 3 dimensional array.
A = rand(2,3,4);
size(A)
ans = 1×3
2 3 4
Now suppose we wanted to extract some plane of the array? Thus to form A(1,:,:)? This should be 2-dimensional thing, right?
A(1,:,:)
ans =
ans(:,:,1) = 0.5894 0.7875 0.8829 ans(:,:,2) = 0.8349 0.4246 0.8329 ans(:,:,3) = 0.9120 0.3446 0.5253 ans(:,:,4) = 0.8350 0.9092 0.3368
Hmm. It is not. At least, that does not look like a 2-dimensional array. In fact, MATLAB thinks it is 3-d still.
size(A(1,:,:))
ans = 1×3
1 3 4
Or, suppose we wanted to form the mean, on the SECOND dimension of A?
mean(A,2)
ans =
ans(:,:,1) = 0.7533 0.4986 ans(:,:,2) = 0.6975 0.6879 ans(:,:,3) = 0.5940 0.6914 ans(:,:,4) = 0.6937 0.7024
size(mean(A,2))
ans = 1×3
2 1 4
So in both cases, I started with a 3-dimensional array, operated on it in a way that logically SHOULD have produced a 2-d array.
Squeeze fixes the problem, by dropping out those spurious singleton dimensions. That is,
squeeze(A(1,:,:))
ans = 3×4
0.5894 0.8349 0.9120 0.8350 0.7875 0.4246 0.3446 0.9092 0.8829 0.8329 0.5253 0.3368
size(squeeze(A(1,:,:)))
ans = 1×2
3 4
squeeze(mean(A,2))
ans = 2×4
0.7533 0.6975 0.5940 0.6937 0.4986 0.6879 0.6914 0.7024
size(squeeze(mean(A,2)))
ans = 1×2
2 4
This allows you to get the result you wanted, in the shape you would have expected.
There is one small caveat to remember. All arrays in MATLAB are treated as if they have infinitely many trailing singleton dimensions. So a scalar is actually an array of size 1x1x1x1x1x1x1x1..., and a vector an array of size Nx1x1x1x1..., or 1xNx1x1x1x1... MATLAB never reports those trailing singleton dimensions however, at least not beyond the second dimension. (This is probably a consequence of requiring backwards compatibility all the way back to version 1 when possible, when MATLAB did not have multidimensional arrays at all.) So we see:
V = 1:3;
size(V)
ans = 1×2
1 3
size(V.')
ans = 1×2
3 1
S = 2;
size(S)
ans = 1×2
1 1
Logically, if we apply squeeze to a scalar, we still have a scalar.
size(squeeze(S))
ans = 1×2
1 1
size(squeeze(V.'))
ans = 1×2
3 1
size(squeeze(V))
ans = 1×2
1 3
So the only thing to remember is that squeeze does not convert a 1x3 vector into a 3x1 vector. (In my eyes, I disagree with the design of squeeze in that respect, but I did not write the code.)
W = A(1,1,:)
W =
W(:,:,1) = 0.5894 W(:,:,2) = 0.8349 W(:,:,3) = 0.9120 W(:,:,4) = 0.8350
So W is a vector, but stored in a 3-dimensional form. It has size 1x1x4.
size(W)
ans = 1×3
1 1 4
size(squeeze(W))
ans = 1×2
4 1
So be careful when squeezing vectors, as it is not always perfectly consistent. At least not in my eyes.
Having read all of that, you should now be a master of the squeeze.