How does Squeeze work?

Hello all,
Can anybody explain me what Squeeze is used for? I have read about it in MATLAB help but I am still confused !
Thanks a lot :-)

 Accepted Answer

Matt Fig
Matt Fig on 1 May 2011
In addition to what James said, just look at an example or two. Look what happens to the dimensions of A with squeeze, particularly what happens to the dimension with the 1 (so-called singleton dimensions).
A = rand(6,7,1,2); % size(A) = [6 7 1 2]
size(squeeze(A))
A = rand(1,6,7,2);
size(squeeze(A))
A = rand(6,7,1,2);
size(squeeze(A))
Now try it with two (or more) 1 (singleton) dimensions.
A = rand(6,7,1,2,1,7); % size(A) = [6 7 1 2 1 7]
size(squeeze(A))
A = rand(1,6,1,7,2,1,8); % size(A) = [1 6 1 7 2 1 8]
size(squeeze(A))
A = rand(6,1,7,1,2);
size(squeeze(A))
I think you see the pattern. Now that you see what is going on with the dimensions, look at the data (using simpler examples):
A = reshape(randperm(6),2,1,3) % Look at this data
squeeze(A) % Now look at it.
A = reshape(randperm(6),1,2,3)
squeeze(A)
So wee see that SQUEEZE reads off the columns of A while creating an array with no singleton dimensions.

6 Comments

To be pedantic: the dimensions are only changed if there is at least one dimension from the third onward which is not 1; if so, then the first dimension which is not 1 becomes the first dimension. For example, squeeze(zeros(1,1,1,1,1,4)) becomes zeros(4,1) but squeeze(zeros(1,4)) stays zeros(1,4)
Matt Fig
Matt Fig on 1 May 2011
Good point, thanks Walter.
@Walter .. Sorry for digging the old post. Is there a logical reason for such behavior? I think, having a uniform behavior would have been better. Like, in the example above, both should return 4x1 or 1x4 vector.
Thorsten
Thorsten on 25 Oct 2016
Edited: Thorsten on 25 Oct 2016
There is no absolute "logical" reason for this. The reason is how squeeze is implemented: if the input is a matrix, it is left unchanged. The logic behind this implementation may be that squeezing matrices makes no sense, since there is nothing smaller than a matrix in Matlab.
If this implementation of squeeze causes some trouble in your code or makes you seriously unhappy, you could implement
function b = mysqueeze(a)
that works just like squeeze but w/o the if condition and the else clause in the original implementation
if ~ismatrix(a)
% squeeze a to b
else
b = a;
end
Hi ,
@Matt Fig,
I'm getting an error in line 2 of the following code snippet while trying to implement it in my MATLAB.
A = rand(6,7,1,2); % size(A) = [6 7 1 2]
size(squeeze(A))
A = rand(1,6,7,2);
size(squeeze(A))
A = rand(6,7,1,2);
size(squeeze(A))
@Swati: your code works without error:
A = rand(6,7,1,2); % size(A) = [6 7 1 2]
size(squeeze(A))
ans = 1×3
6 7 2
A = rand(1,6,7,2);
size(squeeze(A))
ans = 1×3
6 7 2
A = rand(6,7,1,2);
size(squeeze(A))
ans = 1×3
6 7 2
If you are getting an error then please show us the complete error message. This means all of the red text.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 1 May 2011

3 votes

The squeeze function is similar to reshape in that it changes the dimensions of a variable without moving any of the data in memory. The total number of elements and their ordering in memory remain the same. For squeeze, the new dimensions are simply the old dimensions with all singleton (==1) dimensions removed (except 2D inputs are left as is).
As for what it is used for, it may be that the result of some algorithm has left you with a 1x2x1x3 matrix and you need to feed that to a function that needs a 2D input. So you could use squeeze on it to get the 2D input as a 2x3 matrix.

Tags

Asked:

on 1 May 2011

Edited:

on 19 Nov 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!