octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Round/curly bracket interchangeability for cell


From: Mike Miller
Subject: Re: Round/curly bracket interchangeability for cell
Date: Mon, 9 Jan 2017 14:21:25 -0800
User-agent: NeoMutt/20161126 (1.7.1)

On Mon, Jan 09, 2017 at 16:03:30 -0600, Daniel J Sebald wrote:
> I just noticed that once a cell variable exists, round brackets may be
> substituted for curly brackets.  I first noticed this when I encountered a
> simple error like the following:
> 
> octave:49> a{1} = 10;
> octave:50> a{2}
> error: a(2): out of bound 1
> 
> Notice that the error message has a round bracket rather than curly bracket,
> in light of the variable 'a' being a cell.  My thinking was that the
> following makes more sense for an error:
> 
> error: a{2}: out of bound 1
> 
> After further examination, I found the following is acceptable:
> 
> octave:54> b{1} = "curly";
> octave:55> b(2) = "round"
> b =
> 
> 
> {
>   [1,1] = curly
>   [1,2] = round
> }
> 
> (Well, first thing, notice two spaces between "b =" and the variable
> contents display, is that a bug?)
> 
> I don't know if this curly/round bracket is a compatibility issue, but I
> would think this is too free use of round brackets and can cause more
> confusion than anything.  I mean, it is difficult enough for someone
> learning the language to comprehend the difference between a cell and an
> array.  Then introduce the idea that if I first use curly bracket, from then
> on I can write lines of code that use the round bracket to index that cell.
> Being a new user, one might type by mistake "()" rather than "{}" and be
> completely confused.  Even an experienced user wouldn't like reading code
> that did such a thing.

Yes, as I recollect this is a compatibility issue.

To further clarify, indexing on cell arrays with round brackets /
parentheses *is* different from indexing with curly brackets / braces,
both are intentionally supported and intentionally mean different
things, as in Matlab:

  >> a = {10, 11, 12};
  >> a{1}
  ans =  10
  >> a(1)
  ans =
  {
    [1,1] = 10
  }

When used as the LHS of an indexed assignment, they are also different:

  >> a(1:3) = 0
  a = 
  {
    [1,1] = 0
    [1,2] = 0
    [1,3] = 0
  }
  >> a{1:3} = 0
  error: invalid assignment to cs-list outside multiple assignment

  >> [a{1:3}] = deal (3, 2, 1)
  a = 
  {
    [1,1] =  3
    [1,2] =  2
    [1,3] =  1
  }

Only in the degenerate case of indexing a single element of the cell
array are they equivalent.

Does that clear things up?

-- 
mike



reply via email to

[Prev in Thread] Current Thread [Next in Thread]