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:
Comma list with leading cell

Subject: Comma list with leading cell

From: Bruno Luong

Date: 23 May, 2009 11:45:02

Message: 1 of 34

This syntax works it is beyond my expectation! Is it documented somewhere?
 
>> [{} 1 2 3]

ans =

    [1] [2] [3]

% To me the correct syntax is

>> [{} {1 2 3}]

Bruno

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 12:45:02

Message: 2 of 34

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv8nju$kvn$1@fred.mathworks.com>...
> This syntax works it is beyond my expectation! Is it documented somewhere?
>
> >> [{} 1 2 3]
>
> ans =
>
> [1] [2] [3]
>
> % To me the correct syntax is
>
> >> [{} {1 2 3}]
>
> Bruno

it is a weel known fact of ML life

...
When MATLAB sees a subscripted assignment to an existing variable, MATLAB checks the class of the right-hand-side against the class of the left-hand-side (LHS) variable. If different, it attempts to convert the right-hand-side (RHS) variable to the type of the LHS class.
...

     ['a' nan]
     [uint8(1) pi]
     [pi uint8(1)]

here some of the tech docs this is referred to

http://www.mathworks.com/support/solutions/en/data/1-6K5PAO/
http://www.mathworks.com/support/solutions/en/data/1-6K5PAO/

us

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 12:54:01

Message: 3 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv8r4e$19v$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv8nju$kvn$1@fred.mathworks.com>...
> > This syntax works it is beyond my expectation! Is it documented somewhere?
> >
> > >> [{} 1 2 3]
> >
> > ans =
> >
> > [1] [2] [3]
> >
> > % To me the correct syntax is
> >
> > >> [{} {1 2 3}]
> >
> > Bruno
>
> it is a weel known fact of ML life
>
> ...
> When MATLAB sees a subscripted assignment to an existing variable, MATLAB checks the class of the right-hand-side against the class of the left-hand-side (LHS) variable. If different, it attempts to convert the right-hand-side (RHS) variable to the type of the LHS class.
> ...
>
> ['a' nan]
> [uint8(1) pi]
> [pi uint8(1)]
>
> here some of the tech docs this is referred to
>
> http://www.mathworks.com/support/solutions/en/data/1-6K5PAO/
> http://www.mathworks.com/support/solutions/en/data/1-6K5PAO/
>
> us

btw, we've been using this sometimes to bypass NUM2CELL...
it seems a bit faster...

us

Subject: Comma list with leading cell

From: Bruno Luong

Date: 23 May, 2009 12:59:02

Message: 4 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv8r4e$19v$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv8nju$kvn$1@fred.mathworks.com>...
> > This syntax works it is beyond my expectation! Is it documented somewhere?
> >
> > >> [{} 1 2 3]
> >
> > ans =
> >
> > [1] [2] [3]
> >
> > % To me the correct syntax is
> >
> > >> [{} {1 2 3}]
> >
> > Bruno
>
> it is a weel known fact of ML life
>
> ...
> When MATLAB sees a subscripted assignment to an existing variable, MATLAB checks the class of the right-hand-side against the class of the left-hand-side (LHS) variable. If different, it attempts to convert the right-hand-side (RHS) variable to the type of the LHS class.
> ...
>
> ['a' nan]
> [uint8(1) pi]
> [pi uint8(1)]
>
> here some of the tech docs this is referred to
>
> http://www.mathworks.com/support/solutions/en/data/1-6K5PAO/
> http://www.mathworks.com/support/solutions/en/data/1-6K5PAO/
>

Humm it is clearer but not perfect, because why this doesn't work:

>> a={1}

a =

    [1]

>> class(a)

ans =

cell

>> a(1)=2
??? Conversion to cell from double is not possible.

I'm still confused. It seems the example I gave above Matlab succeeds to convert somehow double to cell? Why now it can't.

Bruno

Subject: Comma list with leading cell

From: Matt

Date: 23 May, 2009 13:02:01

Message: 5 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv8r4e$19v$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv8nju$kvn$1@fred.mathworks.com>...
> > This syntax works it is beyond my expectation! Is it documented somewhere?
> >
> > >> [{} 1 2 3]
> >
> > ans =
> >
> > [1] [2] [3]
> >
> > % To me the correct syntax is
> >
> > >> [{} {1 2 3}]
> >
> > Bruno
>
> it is a weel known fact of ML life
>
> ...
> When MATLAB sees a subscripted assignment to an existing variable, MATLAB checks the class of the right-hand-side against the class of the left-hand-side (LHS) variable. If different, it attempts to convert the right-hand-side (RHS) variable to the type of the LHS class.
------

I'm not convinced that that explains it, since Bruno's example was not in fact an assignment (there was no LHS variable). Also, if it were a case of a cell on the LHS converting numeric data on the RHS, the following ought to have produced the same effect

>> a={}

a =

     {}

>> a(2:4)=[1 2 3]
??? Conversion to cell from double is not possible.

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 13:17:01

Message: 6 of 34

"Matt " <xys@whatever.com> wrote in message <gv8s49$2ua$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gv8r4e$19v$1@fred.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv8nju$kvn$1@fred.mathworks.com>...
> > > This syntax works it is beyond my expectation! Is it documented somewhere?
> > >
> > > >> [{} 1 2 3]
> > >
> > > ans =
> > >
> > > [1] [2] [3]
> > >
> > > % To me the correct syntax is
> > >
> > > >> [{} {1 2 3}]
> > >
> > > Bruno
> >
> > it is a weel known fact of ML life
> >
> > ...
> > When MATLAB sees a subscripted assignment to an existing variable, MATLAB checks the class of the right-hand-side against the class of the left-hand-side (LHS) variable. If different, it attempts to convert the right-hand-side (RHS) variable to the type of the LHS class.
> ------
>
> I'm not convinced that that explains it, since Bruno's example was not in fact an assignment (there was no LHS variable)...

actually, it is - hidden; the construct is just not using the typical a=b syntax...
anyhow, this doc page may be more useful re combining unlike classes

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html

us

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 13:26:01

Message: 7 of 34

"Bruno Luong"
> Humm it is clearer but not perfect, because why this doesn't work:
> >> a={1}
> a =
> [1]
> >> class(a)
> ans =
> cell
> >> a(1)=2
> ??? Conversion to cell from double is not possible.
>
> I'm still confused. It seems the example I gave above Matlab succeeds to convert somehow double to cell? Why now it can't...

well,
in your first case, ML knows nothing about the content of the container during execution and simply converts all its entries (=within container) according to some (published) rules...

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html

in your second case, ML knows the class of the lhs and tells you that it cannot add another class (=between containers)

somehow, this all makes sense (to me)...
:-)
urs

Subject: Comma list with leading cell

From: Bruno Luong

Date: 23 May, 2009 13:28:01

Message: 8 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv8t0d$rm7$1@fred.mathworks.com>...

>
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html
>

But us, let us reiterate the question. Why conversion from double to cell

C = {1};
C(1) = 2

is not allowed?

Bruno

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 13:41:01

Message: 9 of 34

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv8tl1$81o$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gv8t0d$rm7$1@fred.mathworks.com>...
>
> >
> > http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html
> >
>
> But us, let us reiterate the question. Why conversion from double to cell
>
> C = {1};
> C(1) = 2
>
> is not allowed?
>
> Bruno

well, as the above mentioned doc says
...
Matrices and arrays can be composed of elements of most any MATLAB data type as long as all elements in the matrix are of the same type. If you do include elements of unlike classes when constructing a matrix, MATLAB converts some elements so that all elements of the resulting matrix are of the same type.
...
that's the WITHIN rule DURING construction of a matrix when nothing is known about it, yet...

again

     r=[{} 1 2 3];

1) construct the matrix = WITHIN: [{} 1 2 3];
2) assign the matrix = BETWEEN: r = ANS
   in this case: r has NO subscripts, so it is just the ans...
   if, however, the lhs comes with subscripts, ML checks for consistency
   BETWEEN the two (lhs = rhs) containers...

urs

Subject: Comma list with leading cell

From: Matt

Date: 23 May, 2009 13:51:01

Message: 10 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv8th9$166$1@fred.mathworks.com>...
> "Bruno Luong"
> > Humm it is clearer but not perfect, because why this doesn't work:
> > >> a={1}
> > a =
> > [1]
> > >> class(a)
> > ans =
> > cell
> > >> a(1)=2
> > ??? Conversion to cell from double is not possible.
> >
> > I'm still confused. It seems the example I gave above Matlab succeeds to convert somehow double to cell? Why now it can't...
>
> well,
> in your first case, ML knows nothing about the content of the container during execution and simply converts all its entries (=within container) according to some (published) rules...
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html

No, this page specifically gives a table of 5 data types for which mixed-type concatenation is valid. Type 'cell' is not among them

Subject: Comma list with leading cell

From: Matt

Date: 23 May, 2009 14:04:02

Message: 11 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv8udd$pdc$1@fred.mathworks.com>...

> Matrices and arrays can be composed of elements of most any MATLAB data type as long as all elements in the matrix are of the same type. If you do include elements of unlike classes when constructing a matrix, MATLAB converts some elements so that all elements of the resulting matrix are of the same type.
> ...
> that's the WITHIN rule DURING construction of a matrix when nothing is known about it, yet...

This also doesn't explain why the following does not work
------
>> a=num2cell(eye(2))

a =

    [1] [0]
    [0] [1]

>> b=eye(2)

b =

     1 0
     0 1

>> [a,b]
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 14:12:01

Message: 12 of 34

"Matt " <xys@whatever.com> wrote in message <gv8v05$355$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gv8th9$166$1@fred.mathworks.com>...
> > "Bruno Luong"
> > > Humm it is clearer but not perfect, because why this doesn't work:
> > > >> a={1}
> > > a =
> > > [1]
> > > >> class(a)
> > > ans =
> > > cell
> > > >> a(1)=2
> > > ??? Conversion to cell from double is not possible.
> > >
> > > I'm still confused. It seems the example I gave above Matlab succeeds to convert somehow double to cell? Why now it can't...
> >
> > well,
> > in your first case, ML knows nothing about the content of the container during execution and simply converts all its entries (=within container) according to some (published) rules...
> >
> > http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html
>
> No, this page specifically gives a table of 5 data types for which mixed-type concatenation is valid. Type 'cell' is not among them

yes, but - like other logical/numeric/char data objects - it is an indexed(!) based container...

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-47534.html

in contrast, just try

     [struct('a','a'),1,2]

again, this whole thing - always - made a lot of sense to me...
however, in a way i can also agree with you two well-known CSSM-syntax purists:
rather than implementing a array-construction-rule-for-unlike-data-objects, TMW might as well just have decided to error out by default if such a construct is encountered during runtime...

urs

Subject: Comma list with leading cell

From: Bruno Luong

Date: 23 May, 2009 14:19:01

Message: 13 of 34

Still not understand. Us seems the only one who understand what is going on.

Just few more examples:

>> C=[{1} {2} 3]

C =

    [1] [2] [3]

>> isequal(C,{1 2 3})

ans =

     1

>> isequal(C,[{1} {2} {3}])

ans =

     1

>> S = [struct() struct()]

S =

1x2 struct array with no fields.

>> S = [struct() 1]
??? Error using ==> horzcat
The following error occurred converting from double to struct:
Error using ==> struct
Conversion to struct from double is not possible.
 
>> C = [{} 1]

C =

    [1]

>>

Confused Bruno

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 14:20:02

Message: 14 of 34

"Matt " <xys@whatever.com> wrote in message <gv8voi$kg2$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gv8udd$pdc$1@fred.mathworks.com>...
>
> > Matrices and arrays can be composed of elements of most any MATLAB data type as long as all elements in the matrix are of the same type. If you do include elements of unlike classes when constructing a matrix, MATLAB converts some elements so that all elements of the resulting matrix are of the same type.
> > ...
> > that's the WITHIN rule DURING construction of a matrix when nothing is known about it, yet...
>
> This also doesn't explain why the following does not work
> ------
> >> a=num2cell(eye(2))
>
> a =
>
> [1] [0]
> [0] [1]
>
> >> b=eye(2)
>
> b =
>
> 1 0
> 0 1
>
> >> [a,b]
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.

but - matt - this is another story, altogether, as YOU very well know...

:-)
urs

Subject: Comma list with leading cell

From: Bruno Luong

Date: 23 May, 2009 14:23:01

Message: 15 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv907h$jee$1@fred.mathworks.com>...

>
> again, this whole thing - always - made a lot of sense to me...

Good for you, then here a little test. Without trying, do you know what will happen with this:

C = [1 2 3 {}]

Bruno

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 14:29:02

Message: 16 of 34

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv90kl$ek3$1@fred.mathworks.com>...
> Still not understand. Us seems the only one who understand what is going on.
>
> Just few more examples:
>
> >> C=[{1} {2} 3]
>
> C =
>
> [1] [2] [3]
>
> >> isequal(C,{1 2 3})
>
> ans =
>
> 1
>
> >> isequal(C,[{1} {2} {3}])
>
> ans =
>
> 1
>
> >> S = [struct() struct()]
>
> S =
>
> 1x2 struct array with no fields.
>
> >> S = [struct() 1]
> ??? Error using ==> horzcat
> The following error occurred converting from double to struct:
> Error using ==> struct
> Conversion to struct from double is not possible.
>
> >> C = [{} 1]
>
> C =
>
> [1]
>
> >>
>
> Confused Bruno

but, bruno, that is EXACTLY(!?!) what i'd expect...
urs

ps: i am clearly NOT brainwashed by TMW, as my recent CSSM track-record (re recent hoodlumish contest) clearly documents...

Subject: Comma list with leading cell

From: Bruno Luong

Date: 23 May, 2009 14:38:01

Message: 17 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv917e$l86$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv90kl$ek3$1@fred.mathworks.com>...

>
> ps: i am clearly NOT brainwashed by TMW

Yes you do... ;-) You, Jos, Matt Fig, Matt, John, Roger, James, Yi Cao, Peter, and few others...

Bruno

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 14:43:01

Message: 18 of 34

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv90s5$sqo$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gv907h$jee$1@fred.mathworks.com>...
>
> >
> > again, this whole thing - always - made a lot of sense to me...
>
> Good for you, then here a little test. Without trying, do you know what will happen with this:
>
> C = [1 2 3 {}]
>
> Bruno

yes, of course,...
;-)

us

Subject: Comma list with leading cell

From: Bruno Luong

Date: 23 May, 2009 14:46:01

Message: 19 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv921l$cvv$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv90s5$sqo$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <gv907h$jee$1@fred.mathworks.com>...
> >
> > >
> > > again, this whole thing - always - made a lot of sense to me...
> >
> > Good for you, then here a little test. Without trying, do you know what will happen with this:
> >
> > C = [1 2 3 {}]
> >
> > Bruno
>
> yes, of course,...
> ;-)
>

Too strong... hat!

Bruno, still confused

Subject: Comma list with leading cell

From: Matt

Date: 23 May, 2009 14:51:01

Message: 20 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv907h$jee$1@fred.mathworks.com>...
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html
> >
> > No, this page specifically gives a table of 5 data types for which mixed-type concatenation is valid. Type 'cell' is not among them
>
> yes, but - like other logical/numeric/char data objects - it is an indexed(!) based container...

But there is nothing in the page you referenced (that I found) that the concatenation rule applies generally to indexed based classes. It says that there are exactly 5 types for which it is applies:

"The following table shows the five classes you can concatenate with an unlike type without generating an error (that is, with the exception of character and logical). "

Subject: Comma list with leading cell

From: Matt

Date: 23 May, 2009 15:14:02

Message: 21 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv90mi$i8h$1@fred.mathworks.com>...

> > This also doesn't explain why the following does not work
> > ------
> > >> a=num2cell(eye(2))
> >
> > a =
> >
> > [1] [0]
> > [0] [1]
> >
> > >> b=eye(2)
> >
> > b =
> >
> > 1 0
> > 0 1
> >
> > >> [a,b]
> > ??? Error using ==> horzcat
> > CAT arguments dimensions are not consistent.
>
> but - matt - this is another story, altogether, as YOU very well know...

As far as I can tell, urs, this is not documented concatenation behavior, so I really don't know why this is another story or what to expect at all. It seems clear after experimentation that @horzcat/cell() will always transform whole matrices to cells


>> [{},uint8(eye(2)), single(eye(2))]

ans =

    [2x2 uint8] [2x2 single]

but that seems very different from concat operations like the following where the transformation is done element by element

>> [uint16(eye(2)), uint8(eye(2))], whos ans
Warning: Concatenation with dominant (left-most) integer class may overflow other operands on
conversion to return class.

ans =

      1 0 1 0
      0 1 0 1

  Name Size Bytes Class Attributes

  ans 2x4 16 uint16

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 15:19:01

Message: 22 of 34

"Matt " <xys@whatever.com> wrote in message <gv92gl$ce4$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gv907h$jee$1@fred.mathworks.com>...
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html
> > >
> > > No, this page specifically gives a table of 5 data types for which mixed-type concatenation is valid. Type 'cell' is not among them
> >
> > yes, but - like other logical/numeric/char data objects - it is an indexed(!) based container...
>
> But there is nothing in the page you referenced (that I found) that the concatenation rule applies generally to indexed based classes. It says that there are exactly 5 types for which it is applies:
>
> "The following table shows the five classes you can concatenate with an unlike type without generating an error (that is, with the exception of character and logical). "

but, matt!
implicit, explicit...

again, bruno, matt, let me reiterate this train of thoughts:

- i understand (at least, i think so; and our coding history has shown so) the array-creation type casting bit (within vs between)...
- i understand that the very syntax purists (like the -obvious- two of you) would rather see an early bailout during creation of arrays of unlike data objects...
- it is a fact of life (again), that TMW decided (a very long time ago) to do it this way (at least they published some rules), which CSSMers have to live with...

urs

Subject: Comma list with leading cell

From: Matt

Date: 23 May, 2009 15:35:02

Message: 23 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv9455$m63$1@fred.mathworks.com>...

> - i understand that the very syntax purists (like the -obvious- two of you) would rather see an early bailout during creation of arrays of unlike data objects...
-----

Not me. I much prefer silent casting.

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 15:48:01

Message: 24 of 34

"Matt " <xys@whatever.com> wrote in message <gv9536$jgf$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gv9455$m63$1@fred.mathworks.com>...
>
> > - i understand that the very syntax purists (like the -obvious- two of you) would rather see an early bailout during creation of arrays of unlike data objects...
> -----
>
> Not me. I much prefer silent casting.

hmm - [mat,'t'] - but that IS exactly what 1) this thread is about and 2) what ML 'tries' to do according to the published rules...

us

Subject: Comma list with leading cell

From: Matt

Date: 23 May, 2009 15:59:02

Message: 25 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv95rh$73o$1@fred.mathworks.com>...
> "Matt " <xys@whatever.com> wrote in message <gv9536$jgf$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <gv9455$m63$1@fred.mathworks.com>...
> >
> > > - i understand that the very syntax purists (like the -obvious- two of you) would rather see an early bailout during creation of arrays of unlike data objects...
> > -----
> >
> > Not me. I much prefer silent casting.
>
> hmm - [mat,'t'] - but that IS exactly what 1) this thread is about and 2) what ML 'tries' to do according to the published rules...

No, the question originally posed by Bruno is about whether the result of [{} 1 2 3] is documented behavior of horzcat(). I still hold that it is not (or rather that I haven't seen the doc).

You seem to think it is inferable that conversion of a matrix to a cell should result in a single cell, as we've seen in expressions like

 
[{}, eye(3)]

ans =

    [3x3 double]


But without documentation to the contrary, I still don't see why it shouldn't/couldn't have resulted in

ans =

    [1] [0] [0]
    [0] [1] [0]
    [0] [0] [1]

Subject: Comma list with leading cell

From: us

Date: 23 May, 2009 16:27:01

Message: 26 of 34

"Matt " <xys@whatever.com> wrote in message <gv96g6$gvu$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <gv95rh$73o$1@fred.mathworks.com>...
> > "Matt " <xys@whatever.com> wrote in message <gv9536$jgf$1@fred.mathworks.com>...
> > > "us " <us@neurol.unizh.ch> wrote in message <gv9455$m63$1@fred.mathworks.com>...
> > >
> > > > - i understand that the very syntax purists (like the -obvious- two of you) would rather see an early bailout during creation of arrays of unlike data objects...
> > > -----
> > >
> > > Not me. I much prefer silent casting.
> >
> > hmm - [mat,'t'] - but that IS exactly what 1) this thread is about and 2) what ML 'tries' to do according to the published rules...
>
> No, the question originally posed by Bruno is about whether the result of [{} 1 2 3] is documented behavior of horzcat(). I still hold that it is not (or rather that I haven't seen the doc).
>
> You seem to think it is inferable that conversion of a matrix to a cell should result in a single cell, as we've seen in expressions like
>
>
> [{}, eye(3)]
>
> ans =
>
> [3x3 double]
>
>
> But without documentation to the contrary, I still don't see why it shouldn't/couldn't have resulted in
>
> ans =
>
> [1] [0] [0]
> [0] [1] [0]
> [0] [0] [1]

hmm... mat...

     [{}, eye(3)]
% yields
% ans = [3x3 double]
% which is the same as -> eye(3) WITHIN a matrix of cells {} -> {eye(3)}
     {eye(3)}
% ans = [3x3 double]

i just don't see what is wrong with this EXPECTED behavior...

urs

Subject: Comma list with leading cell

From: Matt Fig

Date: 23 May, 2009 16:40:18

Message: 27 of 34

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv8nju$kvn$1@fred.mathworks.com>...
> This syntax works it is beyond my expectation! Is it documented somewhere?
>
> >> [{} 1 2 3]
>
> ans =
>
> [1] [2] [3]
>
> % To me the correct syntax is
>
> >> [{} {1 2 3}]
>
> Bruno


Wow, I think num2cell is going to see a little less runtime around my PC! I don't quite understand all of the subtleties of the other examples in this thread yet (I just woke up!), but that is a cool discovery. What were you trying to do when you found that?

Subject: Comma list with leading cell

From: Matt

Date: 23 May, 2009 16:45:02

Message: 28 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv984l$s21$1@fred.mathworks.com>...
> "Matt " <xys@whatever.com> wrote in message <gv96g6$gvu$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <gv95rh$73o$1@fred.mathworks.com>...
> > > "Matt " <xys@whatever.com> wrote in message <gv9536$jgf$1@fred.mathworks.com>...
> > > > "us " <us@neurol.unizh.ch> wrote in message <gv9455$m63$1@fred.mathworks.com>...
> > > >
> > > > > - i understand that the very syntax purists (like the -obvious- two of you) would rather see an early bailout during creation of arrays of unlike data objects...
> > > > -----
> > > >
> > > > Not me. I much prefer silent casting.
> > >
> > > hmm - [mat,'t'] - but that IS exactly what 1) this thread is about and 2) what ML 'tries' to do according to the published rules...
> >
> > No, the question originally posed by Bruno is about whether the result of [{} 1 2 3] is documented behavior of horzcat(). I still hold that it is not (or rather that I haven't seen the doc).
> >
> > You seem to think it is inferable that conversion of a matrix to a cell should result in a single cell, as we've seen in expressions like
> >
> >
> > [{}, eye(3)]
> >
> > ans =
> >
> > [3x3 double]
> >
> >
> > But without documentation to the contrary, I still don't see why it shouldn't/couldn't have resulted in
> >
> > ans =
> >
> > [1] [0] [0]
> > [0] [1] [0]
> > [0] [0] [1]
>
> hmm... mat...
>
> [{}, eye(3)]
> % yields
> % ans = [3x3 double]
> % which is the same as -> eye(3) WITHIN a matrix of cells {} -> {eye(3)}
> {eye(3)}
> % ans = [3x3 double]
>

Are you saying that horzcat(A,B,C,...) is equivalent to {A,B,C,...} if one of the arguments is a cell array? Apparently not, since

>>horzcat({},eye(3))
ans =

    [3x3 double]

but

>> {{},eye(3)}

ans =

     {} [3x3 double]

Subject: Comma list with leading cell

From: Bruno Luong

Date: 23 May, 2009 19:05:02

Message: 29 of 34

What bother me usr is this. When we type

[1:3 4 5]

it gave the array of 1 to 5.

Now, what happens if I do

[1:3 4 {} 5]

Because of the presence of {}, Matlab will interpret (or CAST or what whatever the right world)

1:3 -> {1:3}
4 -> {4}
5 -> {5}

So it looks like MATLAB is able to convert DOUBLE array to CELL array implicitly in this build.

So when I type

>> C = {1}
>> C(1) = 2

I would expect it behaves a little bit like

>> C=uint8(1)
>> C(1) = pi

where cast from DOUBLE to INT8 is carried out.

I know that MATLAB *can* cast from class DOUBLE to CELL (while building an array), but not here: it flushes me out with the error message. To me this is an inconsistent in MATLAB behavior.

And I agree with Matt, from many documents you have showed, none of them address this behavior, even that you think the contrary.

Anyway, I might change my opinion later, but for now I'm not yet convinced.

Thanks, it was a good discussion.

Bruno

Subject: Comma list with leading cell

From: Steven Lord

Date: 24 May, 2009 01:56:11

Message: 30 of 34


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:gv8rum$lea$1@fred.mathworks.com...
> "us " <us@neurol.unizh.ch> wrote in message
> <gv8r4e$19v$1@fred.mathworks.com>...
>> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>> <gv8nju$kvn$1@fred.mathworks.com>...
>> > This syntax works it is beyond my expectation! Is it documented
>> > somewhere?
>> >
>> > >> [{} 1 2 3]
>> >
>> > ans =
>> >
>> > [1] [2] [3]

This attempts to concatenate together a cell array, a double, a double, and
a double. In order to do so, they need to be the same data type; since the
left-most element being concatenated is a cell array, the other elements are
turned into cell arrays, and then the four cell arrays are concatenated
together using normal cell array concatenation.

*snip*

>> > Bruno
>>
>> it is a weel known fact of ML life
>>
>> ...
>> When MATLAB sees a subscripted assignment to an existing variable, MATLAB
>> checks the class of the right-hand-side against the class of the
>> left-hand-side (LHS) variable. If different, it attempts to convert the
>> right-hand-side (RHS) variable to the type of the LHS class.

While true, that's irrelevant in this case, as we're dealing with
concatenation not assignment.

>> ['a' nan]
>> [uint8(1) pi]
>> [pi uint8(1)]
>>
>> here some of the tech docs this is referred to
>>
>> http://www.mathworks.com/support/solutions/en/data/1-6K5PAO/
>> http://www.mathworks.com/support/solutions/en/data/1-6K5PAO/
>>
>
> Humm it is clearer but not perfect, because why this doesn't work:
>
>>> a={1}
>
> a =
>
> [1]
>
>>> class(a)
>
> ans =
>
> cell
>
>>> a(1)=2
> ??? Conversion to cell from double is not possible.
>
> I'm still confused. It seems the example I gave above Matlab succeeds to
> convert somehow double to cell? Why now it can't.

The rules for concatenation and for subscripted assignment are different.
It's not ideal, but that's how it works.

--
Steve Lord
slord@mathworks.com

Subject: Comma list with leading cell

From: Bruno Luong

Date: 24 May, 2009 06:39:02

Message: 31 of 34

"Steven Lord" <slord@mathworks.com> wrote in message <gva9fe$egf$1@fred.mathworks.com>...
>

>
> The rules for concatenation and for subscripted assignment are different.
> It's not ideal, but that's how it works.
>

That says all. A consistent answer for a non-consistent behavior. Thanks for shedding a light Steve.

Bruno

Subject: Comma list with leading cell

From: us

Date: 24 May, 2009 09:05:03

Message: 32 of 34

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gvaq26$7jb$1@fred.mathworks.com>...
> "Steven Lord" <slord@mathworks.com> wrote in message <gva9fe$egf$1@fred.mathworks.com>...
> >
>
> >
> > The rules for concatenation and for subscripted assignment are different.
> > It's not ideal, but that's how it works.
> >
>
> That says all. A consistent answer for a non-consistent behavior. Thanks for shedding a light Steve.
>
> Bruno

well, yes, quite what i said using the expression within/between...

:-)
us

Subject: Comma list with leading cell

From: Matt

Date: 24 May, 2009 17:19:02

Message: 33 of 34

"Steven Lord" <slord@mathworks.com> wrote in message <gva9fe$egf$1@fred.mathworks.com>...

> This attempts to concatenate together a cell array, a double, a double, and
> a double. In order to do so, they need to be the same data type; since the
> left-most element being concatenated is a cell array, the other elements are
> turned into cell arrays, and then the four cell arrays are concatenated
> together using normal cell array concatenation.

Thanks, Steve. But what we were wondering, initially, is if this is documented anywhere. I see nothing in "doc horzcat" describing the rule for converting doubles to cells. Apparently, the conversion rule is @(x) {x}, but it could also have been @(x) num2cell(x) for example.

And, according to this web page referenced above by urs, it shouldn't even be possible.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html

According to that page, the only mixture of data types that supported for concatenation are char, double,single,logical, and integer types.


 

Subject: Comma list with leading cell

From: Kim

Date: 21 Mar, 2010 07:34:11

Message: 34 of 34

"us " <us@neurol.unizh.ch> wrote in message <gv8udd$pdc$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gv8tl1$81o$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <gv8t0d$rm7$1@fred.mathworks.com>...
> >
> > >
> > > http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html
http://www.wikio.com/article/bad-credit-payday-loans-176415445
> > >
> >
> > But us, let us reiterate the question. Why conversion from double to cell
> >
> > C = {1};
> > C(1) = 2
> >
> > is not allowed?
> >
> > Bruno
>
> well, as the above mentioned doc says
> ...
> Matrices and arrays can be composed of elements of most any MATLAB data type as long as all elements in the matrix are of the same type. If you do include elements of unlike classes when constructing a matrix, MATLAB converts some elements so that all elements of the resulting matrix are of the same type.
> ...
> that's the WITHIN rule DURING construction of a matrix when nothing is known about it, yet...
>
> again
>
> r=[{} 1 2 3];
>
> 1) construct the matrix = WITHIN: [{} 1 2 3];
> 2) assign the matrix = BETWEEN: r = ANS
> in this case: r has NO subscripts, so it is just the ans...
> if, however, the lhs comes with subscripts, ML checks for consistency
> BETWEEN the two (lhs = rhs) containers...
>
> urs

Tags for 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