row-column confusion with squeeze

조회 수: 5 (최근 30일)
Abdullah Fahim
Abdullah Fahim 2016년 10월 25일
댓글: Abdullah Fahim 2016년 10월 26일
Can anybody please explain why A has 3x1 dimension whereas B has 2x3 dimension?
I mean, in both cases, I put 3 as the last dimension in the 3D matrix. So, both A and B should have either 3 rows or 3 columns, right?
If the argument is, squeeze just get rid of all the single-tone dimensions without keeping the relative position, then A and C should be of same dimension? But that is not the case as well.
A=squeeze(zeros(1,1,3))
A =
0
0
0
B=squeeze(zeros(1,2,3))
B =
0 0 0
0 0 0
C=squeeze(zeros(1,3,1))
C =
0 0 0
  댓글 수: 6
David Goodmanson
David Goodmanson 2016년 10월 25일
Probably not real logical. It seems reasonable that (3,1,1,1) should become a column vector of size (3,1) (which happens) and (1,1,1,3) should become a row vector of size (1,3) (which doesn't happen). But then should ( [m ones] 3 [n ones] ) become a row vector or a column vector?
Abdullah Fahim
Abdullah Fahim 2016년 10월 26일
Good question.. I have no idea :). I got your point, so dropping the subject.

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

답변 (1개)

Thorsten
Thorsten 2016년 10월 25일
편집: Thorsten 2016년 10월 25일
Squeeze squeezes out all singleton dimensions of X, if X is not a matrix.
In case of
C = squeeze(zeros(1,3,1))
whos C
Name Size Bytes Class Attributes
C 1x3 24 double
you already have a matrix, namely a 1 x 3 matrix. The trailing singleton dimensions are ignored by zeros.
isequal(zeros(1,3,1), zeros(1,3))
ans =
1
or
isequal(zeros(1,3,1,1,1,1,1), zeros(1,3))
ans =
1
So in case of C, nothing is squeezed and C is left as is.
In case of A, you don't have a matrix, but a 1 x 1 x 3 array. The first two singleton dimensions are squeezed out, resulting in 3 as the first dimension. Because there is no vector in Matlab, a second dimension is needed, which is 1. So A 1 x 1 x 3 is squeezed to 3 x 1.
In case of B, you have a 1 x 2 x 3 array. The first dimension is a singleton dimension which is squeezed out, resulting in a 2 x 3 matrix.
One could see an inconsistency between the handling of A and C, because the squeezing algorithm is not applied to C, since C is a matrix. But the help of squeeze explicitly states
2-D arrays are unaffected by squeeze so that row vectors remain rows.
BTW, squeeze is not built-in, so you can view the code with
edit squeeze
or
type squeeze
and have a look at how it works.
  댓글 수: 1
Abdullah Fahim
Abdullah Fahim 2016년 10월 26일
Thanks Thorsten. I didn't realize zeros(1,3,1,1,1) === zeros(1,3). Thanks for pointing that out.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by