row-column confusion with squeeze
5 次查看(过去 30 天)
显示 更早的评论
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
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?
回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!