필터 지우기
필터 지우기

How does Squeeze work?

조회 수: 65 (최근 30일)
Negar
Negar 2011년 5월 1일
편집: Stephen23 2020년 11월 19일
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 :-)

채택된 답변

Matt Fig
Matt Fig 2011년 5월 1일
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
Swati Sarangi
Swati Sarangi 2020년 11월 19일
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))
Stephen23
Stephen23 2020년 11월 19일
편집: Stephen23 2020년 11월 19일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

James Tursa
James Tursa 2011년 5월 1일
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.

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by