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:
Matlab squeezes trailing singletons

Subject: Matlab squeezes trailing singletons

From: LP

Date: 18 Aug, 2011 23:59:09

Message: 1 of 6

Hi,

Have you ever noticed;

size( ones( 1,1,1,3 ) ) = [ 1,1,1,3 ];

while

size( ones( 3,1,1,1 ) ) = [ 3,1 ];

Further, many array manipulation routines also squeeze off singletons in a non conforming manner

size ( reshape( ones(1,1,1,3), [ 3,1,1,1] ) ) = [ 3,1]

While it makes sense that singleton dimensions add no information (ie they are the same size in memory) this non uniform behaviour is frustrating for so many reasons.

Cheers, LP



 

Subject: Matlab squeezes trailing singletons

From: Doug Schwarz

Date: 19 Aug, 2011 01:33:54

Message: 2 of 6

In article <j2k90d$pds$1@newscl01ah.mathworks.com>,
 "LP " <lylexxxxxxx@gmail.com> wrote:

> Hi,
>
> Have you ever noticed;
>
> size( ones( 1,1,1,3 ) ) = [ 1,1,1,3 ];
>
> while
>
> size( ones( 3,1,1,1 ) ) = [ 3,1 ];
>
> Further, many array manipulation routines also squeeze off singletons in a
> non conforming manner
>
> size ( reshape( ones(1,1,1,3), [ 3,1,1,1] ) ) = [ 3,1]
>
> While it makes sense that singleton dimensions add no information (ie they
> are the same size in memory) this non uniform behaviour is frustrating for
> so many reasons.
>
> Cheers, LP


If you really need the size of the fourth dimension then use

  size(ones(3,1,1,1),4) = 1

or write a little utility function that appends any necessary 1s:

  mysize(ones(3,1,1,1),4) = [3 1 1 1]

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Matlab squeezes trailing singletons

From: LP

Date: 19 Aug, 2011 01:43:09

Message: 3 of 6

Hi Doug,

unfortunately my problems does not pertain to using the 'size' function but in expecting that the ndims of an array does not change in a much more complicated operation. As it is i just have to handle the edge cases of singletons very carefully... oh well..
Thanks, LP

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-B9576C.21335418082011@news.frontiernet.net>...
> In article <j2k90d$pds$1@newscl01ah.mathworks.com>,
> "LP " <lylexxxxxxx@gmail.com> wrote:
>
> > Hi,
> >
> > Have you ever noticed;
> >
> > size( ones( 1,1,1,3 ) ) = [ 1,1,1,3 ];
> >
> > while
> >
> > size( ones( 3,1,1,1 ) ) = [ 3,1 ];
> >
> > Further, many array manipulation routines also squeeze off singletons in a
> > non conforming manner
> >
> > size ( reshape( ones(1,1,1,3), [ 3,1,1,1] ) ) = [ 3,1]
> >
> > While it makes sense that singleton dimensions add no information (ie they
> > are the same size in memory) this non uniform behaviour is frustrating for
> > so many reasons.
> >
> > Cheers, LP
>
>
> If you really need the size of the fourth dimension then use
>
> size(ones(3,1,1,1),4) = 1
>
> or write a little utility function that appends any necessary 1s:
>
> mysize(ones(3,1,1,1),4) = [3 1 1 1]
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Subject: Matlab squeezes trailing singletons

From: Bruno Luong

Date: 19 Aug, 2011 07:17:10

Message: 4 of 6

"LP" wrote in message <j2k90d$pds$1@newscl01ah.mathworks.com>...

>
> While it makes sense that singleton dimensions add no information (ie they are the same size in memory) this non uniform behaviour is frustrating for so many reasons.
>

I agree. The conveniences is overweighted by the inconveniences of such behavior. So far I can still walk away with command that enforce the dimension that should be:

A = rand(3,4,1,1)
nd = 4;
sizeA= ones(1,4)
sizeA(1:ndims(A)) = size(A)

Another similar "feature" that I also keep in mind when programming is that ndims always returns value greater or equal tot 2; and exception of the exception: sometime the trailing 1 reappears in the second position !

Bruno

Subject: Matlab squeezes trailing singletons

From: Matt J

Date: 19 Aug, 2011 08:36:09

Message: 5 of 6

"LP" wrote in message <j2kf3d$cm6$1@newscl01ah.mathworks.com>...
> Hi Doug,
>
> unfortunately my problems does not pertain to using the 'size' function but in expecting that the ndims of an array does not change in a much more complicated operation. As it is i just have to handle the edge cases of singletons very carefully... oh well..
==================

I'm not saying there aren't any cases when this is inconvenient, but you haven't really described any. What exactly is the problem? You know that you can index an n-dimensional array as if it were an m>n dimensional array, right?

>> a=rand(2,2)

a =

    0.3835 0.2411
    0.6331 0.7816

>> a(2,1,1,1)

ans =

    0.6331

Subject: Matlab squeezes trailing singletons

From: LP

Date: 23 Aug, 2011 00:08:08

Message: 6 of 6

Hi Matt,

I am refraining from putting any sample code up because I am not looking for help solving my problem. This thread was more about why Matlab operates this way?

But an example i could site is if i want to conform an array of structs to an array (off the top of my head)..

st(1:100).x = rand(2,2);

x = reshape( [st.x] , [2,2,length(st)])
x = permute(x, circshift(1:ndims(x),[0,1]))

x should look like a [100,2,2]. now look what happens if length(st) =1, you get a [2,2].

Then if i say nt = size(x,1), i get 2 not 1.

Again, edge case can be handled but this is frustrating!! And I also agree with Bruno that representing 1D arrays as 2D with a singleton can lead to some confusing codes/edge cases that would not occur if it was just represented by a 1D array. But i understand the follow-on implications for matrix multiplication so your stuck.

Cheers, Lyle

"Matt J" wrote in message <j2l79p$jp7$1@newscl01ah.mathworks.com>...
> "LP" wrote in message <j2kf3d$cm6$1@newscl01ah.mathworks.com>...
> > Hi Doug,
> >
> > unfortunately my problems does not pertain to using the 'size' function but in expecting that the ndims of an array does not change in a much more complicated operation. As it is i just have to handle the edge cases of singletons very carefully... oh well..
> ==================
>
> I'm not saying there aren't any cases when this is inconvenient, but you haven't really described any. What exactly is the problem? You know that you can index an n-dimensional array as if it were an m>n dimensional array, right?
>
> >> a=rand(2,2)
>
> a =
>
> 0.3835 0.2411
> 0.6331 0.7816
>
> >> a(2,1,1,1)
>
> ans =
>
> 0.6331

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