row-column confusion with squeeze

6 Ansichten (letzte 30 Tage)
Abdullah Fahim
Abdullah Fahim am 25 Okt. 2016
Kommentiert: Abdullah Fahim am 26 Okt. 2016
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 Kommentare
David Goodmanson
David Goodmanson am 25 Okt. 2016
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 am 26 Okt. 2016
Good question.. I have no idea :). I got your point, so dropping the subject.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Thorsten
Thorsten am 25 Okt. 2016
Bearbeitet: Thorsten am 25 Okt. 2016
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 Kommentar
Abdullah Fahim
Abdullah Fahim am 26 Okt. 2016
Thanks Thorsten. I didn't realize zeros(1,3,1,1,1) === zeros(1,3). Thanks for pointing that out.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by