Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Why does ndims(zeros(1,1,1)) equal 2?

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: Alan

Date: 20 Jun, 2011 13:51:04

Message: 1 of 12

Why does ndims(zeros(1,1,1)) equal 2 and not 3?

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: matt dash

Date: 20 Jun, 2011 14:26:04

Message: 2 of 12

"Alan" wrote in message <itnj88$5ka$1@newscl01ah.mathworks.com>...
> Why does ndims(zeros(1,1,1)) equal 2 and not 3?

n = ndims(A) returns the number of dimensions in the array A. The number of dimensions in an array is always greater than or equal to 2. Trailing singleton dimensions are ignored. A singleton dimension is any dimension for which size(A,dim) = 1.

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: Steven_Lord

Date: 20 Jun, 2011 14:27:15

Message: 3 of 12



"Alan " <alan.newsgroup.acount@gmail.com> wrote in message
news:itnj88$5ka$1@newscl01ah.mathworks.com...
> Why does ndims(zeros(1,1,1)) equal 2 and not 3?

Because trailing singleton dimensions are ignored, as stated in the
documentation page for NDIMS.

http://www.mathworks.com/help/techdoc/ref/ndims.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: Alan

Date: 20 Jun, 2011 17:03:04

Message: 4 of 12

> Because trailing singleton dimensions are ignored, as stated in the
> documentation page for NDIMS.
>
> http://www.mathworks.com/help/techdoc/ref/ndims.html


So the follow-up question would be why are trailing singleton dimensions ignored? I have a test case where I supply a 3D matrix to a function. I have to write extra code just to deal with the special case that the last dimension is 1.

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: Florin Neacsu

Date: 20 Jun, 2011 17:16:20

Message: 5 of 12

"Alan" wrote in message <itnug8$c3e$1@newscl01ah.mathworks.com>...
> > Because trailing singleton dimensions are ignored, as stated in the
> > documentation page for NDIMS.
> >
> > http://www.mathworks.com/help/techdoc/ref/ndims.html
>
>
> So the follow-up question would be why are trailing singleton dimensions ignored? I have a test case where I supply a 3D matrix to a function. I have to write extra code just to deal with the special case that the last dimension is 1.

Hi,


Have a look at squeeze. It might help you.

Regards,
Florin

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: Steven_Lord

Date: 20 Jun, 2011 17:23:39

Message: 6 of 12



"Alan " <alan.newsgroup.acount@gmail.com> wrote in message
news:itnug8$c3e$1@newscl01ah.mathworks.com...
>> Because trailing singleton dimensions are ignored, as stated in the
>> documentation page for NDIMS.
>>
>> http://www.mathworks.com/help/techdoc/ref/ndims.html
>
>
> So the follow-up question would be why are trailing singleton dimensions
> ignored? I have a test case where I supply a 3D matrix to a function. I
> have to write extra code just to deal with the special case that the last
> dimension is 1.

Any m-by-n matrix can be treated as an m-by-n-by-1-by-1-by-...-by-1 array
for indexing purposes.

M = reshape(1:12, 3, 4);
M(2, 3) % 8
M(2, 3, 1) % also 8
M(2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1) % also 8

So if you asked for the size of M, what is the "right" number of trailing
singleton dimensions to include? None past dimension 2 is the answer we've
chosen, and so size(M) returns [3 4].

How do you handle the 3D array case in your code when the size in the 3rd
dimension is not 1? Do you do this:

S = size(A);
S(3) % Fails if A is a matrix

or do you do something like:

for whichPage = 1:size(A, 3); % Works even if A is a matrix

For more discussion about this type of behavior in the context of the SIZE
function, take a look at this post on Steve's blog:

http://blogs.mathworks.com/steve/2011/03/29/even-more-information-about-the-size-function/

and the post before that one (linked in the first paragraph of that post.)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: Matt J

Date: 20 Jun, 2011 17:32:04

Message: 7 of 12

"Alan" wrote in message <itnug8$c3e$1@newscl01ah.mathworks.com>...
> > Because trailing singleton dimensions are ignored, as stated in the
> > documentation page for NDIMS.
> >
> > http://www.mathworks.com/help/techdoc/ref/ndims.html
>
>
> So the follow-up question would be why are trailing singleton dimensions ignored? I have a test case where I supply a 3D matrix to a function. I have to write extra code just to deal with the special case that the last dimension is 1.
==================

I think the real follow-up question is, why are you using NDIMS if you always plan to treat the input as a 3D array? There's no reason why zeros(1,1,1) can't be manipulated in the same manner as any other 3D array.

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: James Tursa

Date: 20 Jun, 2011 18:04:04

Message: 8 of 12

"Matt J" wrote in message <ito06k$hn3$1@newscl01ah.mathworks.com>...
> "Alan" wrote in message <itnug8$c3e$1@newscl01ah.mathworks.com>...
> > > Because trailing singleton dimensions are ignored, as stated in the
> > > documentation page for NDIMS.
> > >
> > > http://www.mathworks.com/help/techdoc/ref/ndims.html
> >
> >
> > So the follow-up question would be why are trailing singleton dimensions ignored? I have a test case where I supply a 3D matrix to a function. I have to write extra code just to deal with the special case that the last dimension is 1.
> ==================
>
> I think the real follow-up question is, why are you using NDIMS if you always plan to treat the input as a 3D array? There's no reason why zeros(1,1,1) can't be manipulated in the same manner as any other 3D array.

Follow-up note: For the zeros(1,1,1) example (and I am sure others as well) the resulting variable is exactly the same as the result of a zeros(1,1) call internally. That is, the 3rd dimension isn't even physically stored in the internal variable structure, you actually get a 2D result. But as Steven and Matt already point out, that does not prevent you from indexing as many dimensions as you want as long as the trailing indexes beyond the 2nd index are all 1.

James Tursa

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: James Tursa

Date: 20 Jun, 2011 19:23:02

Message: 9 of 12

"James Tursa" wrote in message <ito22k$nq0$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <ito06k$hn3$1@newscl01ah.mathworks.com>...
> > "Alan" wrote in message <itnug8$c3e$1@newscl01ah.mathworks.com>...
> > > > Because trailing singleton dimensions are ignored, as stated in the
> > > > documentation page for NDIMS.
> > > >
> > > > http://www.mathworks.com/help/techdoc/ref/ndims.html
> > >
> > >
> > > So the follow-up question would be why are trailing singleton dimensions ignored? I have a test case where I supply a 3D matrix to a function. I have to write extra code just to deal with the special case that the last dimension is 1.
> > ==================
> >
> > I think the real follow-up question is, why are you using NDIMS if you always plan to treat the input as a 3D array? There's no reason why zeros(1,1,1) can't be manipulated in the same manner as any other 3D array.
>
> Follow-up note: For the zeros(1,1,1) example (and I am sure others as well) the resulting variable is exactly the same as the result of a zeros(1,1) call internally. That is, the 3rd dimension isn't even physically stored in the internal variable structure, you actually get a 2D result. But as Steven and Matt already point out, that does not prevent you from indexing as many dimensions as you want as long as the trailing indexes beyond the 2nd index are all 1.
>
> James Tursa

I will provide another follow-up note regarding specifically the behavior of the ndims and size functions. As stated in the online doc for ndims:

"Trailing singleton dimensions are ignored. A singleton dimension is any dimension for which size(A,dim) = 1."

This is false. The function ndims simply returns the number of dimensions that are physically present in the variable, and those dimensions are returned by the size function. If any trailing dimensions are physically present and equal to 1, they will be reported by the result of the ndims function inspite of the documentation. The stated algorithm:

"ndims(x) is length(size(x))"

does appear to match the actual behavior of ndims, since the size function *does* in fact return all physical dimensions present, including all trailing dimensions that are equal to 1 beyond the 2nd dimension.

What is subtle is that many (most? all?) MATLAB functions that can return an nD result apparently check to see if there are any trailing dimensions equal to 1 (beyond the 2nd dimension) and if so they are lopped off and only those dimensions prior to the trailing singleton dimensions are actually stored in the return variable. Then any subsequent ndims or size function called on that returned variable will not show these trailing singleton dimensions because they are not physically present.

That being said, if one were to construct a variable that had the trailing singleton dimensions physically present in the variable, both ndims and size will report them. I ran some tests on R2006b & R2011a on 32-bit WinXP to verify this. In practice, I don't see how this distinction should affect one's programming ... if your code can accept 2D or nD inputs then code accordingly. I don't *know* of any MATLAB functions that will return an nD result with trailing singleton dimensions present, but I haven't specifically looked either. I constructed my test example in a mex routine and frankly am not sure if such a variable would break any MATLAB code.

James Tursa

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: Matt J

Date: 20 Jun, 2011 19:44:05

Message: 10 of 12

"James Tursa" wrote in message <ito6mm$aun$1@newscl01ah.mathworks.com>...
>
 I constructed my test example in a mex routine and frankly am not sure if such a variable would break any MATLAB code.
=======================

Have you tried mutiplying two NxNx1 matrices together?
Have you tried ISMATRIX on such an array?

MATLAB needs some way to determine whether an array is capable of linear algebraic matrix operations. Chances are, it checks for the condition ndims=2, which would get ruined by the introduction of trailing 1s.

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: James Tursa

Date: 20 Jun, 2011 20:21:04

Message: 11 of 12

"Matt J" wrote in message <ito7u4$eq3$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <ito6mm$aun$1@newscl01ah.mathworks.com>...
> >
> I constructed my test example in a mex routine and frankly am not sure if such a variable would break any MATLAB code.
> =======================
>
> Have you tried mutiplying two NxNx1 matrices together?
> Have you tried ISMATRIX on such an array?
>
> MATLAB needs some way to determine whether an array is capable of linear algebraic matrix operations. Chances are, it checks for the condition ndims=2, which would get ruined by the introduction of trailing 1s.

Yes, trailing singleton dimensions that are physically present in the variable beyond the 2nd dimension will cause a failure if you attempt matrix multiplication (although my mtimesx FEX routine handles them easily). Also the ismatrix function returns 0. So there is probably an entire set of functionality that would be "broken" by this behavior. This might be expected, however, since other things are well known to be "broken" by similar behavior. E.g., you can't matrix multiply a 1x2x3 by a 3x4 (often the 1x2x3 is the result of an array section operation), and this case regularly comes up in the newsgroup and answers (users are directed to the squeeze et al functions). But your point is well taken, and I would not be surprised to find that *all* MATLAB functions lop off trailing singleton dimensions beyond the 2nd for the return variable(s).

(My original thought for "broken" was that there may be some MATLAB code that would actually seg fault if trailing singleton dimensions beyond the 2nd were present. Of course I don't know the answer to that one.)

James Tursa

Subject: Why does ndims(zeros(1,1,1)) equal 2?

From: James Tursa

Date: 20 Jun, 2011 20:32:02

Message: 12 of 12

"James Tursa" wrote in message <itoa3g$lfm$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <ito7u4$eq3$1@newscl01ah.mathworks.com>...
> > "James Tursa" wrote in message <ito6mm$aun$1@newscl01ah.mathworks.com>...
> > >
> > I constructed my test example in a mex routine and frankly am not sure if such a variable would break any MATLAB code.
> > =======================
> >
> > Have you tried mutiplying two NxNx1 matrices together?
> > Have you tried ISMATRIX on such an array?
> >
> > MATLAB needs some way to determine whether an array is capable of linear algebraic matrix operations. Chances are, it checks for the condition ndims=2, which would get ruined by the introduction of trailing 1s.
>
> Yes, trailing singleton dimensions that are physically present in the variable beyond the 2nd dimension will cause a failure if you attempt matrix multiplication (although my mtimesx FEX routine handles them easily). Also the ismatrix function returns 0. So there is probably an entire set of functionality that would be "broken" by this behavior. This might be expected, however, since other things are well known to be "broken" by similar behavior. E.g., you can't matrix multiply a 1x2x3 by a 3x4 (often the 1x2x3 is the result of an array section operation), and this case regularly comes up in the newsgroup and answers (users are directed to the squeeze et al functions). But your point is well taken, and I would not be surprised to find that *all* MATLAB functions lop off trailing singleton dimensions beyond the 2nd for the return variable(s).
>
> (My original thought for "broken" was that there may be some MATLAB code that would actually seg fault if trailing singleton dimensions beyond the 2nd were present. Of course I don't know the answer to that one.)
>
> James Tursa

I will add one final comment that, specifically in the matrix multiply case, this would not be any more "broken" than Fortran, since Fortran would also not allow the intrinsic matmul function to operate on nD arrays for n > 2. Same as MATLAB, Fortran would force you to specifically create a 2D array section version of the variable first.

James Tursa

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us