MATLAB: How does Squeeze work

squeeze

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 🙂

Best Answer

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.