Re: Increment operator



In article <MPG.1dc2a9e5bc356489899cf@xxxxxxxxxxxxxxxxxx>,
Loren Shure <loren.shure@xxxxxxxxxxxxx> wrote:

> In article <see-71ED10.10482220102005@xxxxxxxxxxxxxxxxxxxx>,
> see@xxxxxxxxxxxxxxxxxxx says...
[snip]
> > ... I'll bet *most* people would be happy if
> >
> > <A> += 1
> >
> > does the same thing as
> >
> > <A> = <A> + 1
> >
> > for all possible expressions, <A>. For example,
> >
> > a(k) += 1 is equivalent to a(k) = a(k) + 1
> >
> > a.field += 1 is equivalent to a.field = a.field + 1

[snip]

>
> But there are 2 credible ways (maybe more?) to look at the expression
> Steve wrote -- which is the problem: a(k) += 1
>
> if k has repeated indices -- which is what Steve was trying to get at.
> They both map to reasonable behavior.
>
> 1) a(k) = a(k) + 1;
> for repeated k, a(k) would only increase by 1
> 2) for ind = 1:length(k)
> a(k(i)) = a(k(i)) + 1;
> end
> for repeated k, a(k) would have the +1 statements accumulate
>
> So that's the main conundrum - what behavior to have with repeated
> indices. We've asked before and gotten mixed answers before. Feel free
> to add your own thoughts now.
>
> --Loren

Hi Loren,

I understand completely about the repeated indices. I think, for
simplicity, that MATLAB should treat += as I described above. In other
words, all the += will do is obviate writing a complicated expression
twice, but will do the same thing as if it was there twice, warts and
all. So,

<expression 1> += <expression 2>

will do the same thing as

<expression 1> = <expression 1> + <expression 2>

always, including evaluating <expression 1> twice unless you know that
it won't make a difference. (In general, an index could be a function
evaluation with side effects.)

I don't think anyone asking for this feature wants to do anything fancy
with accumulation. You have to define what it will do in these unusual
cases, of course, but I don't think it matters very much as long as it
is spelled out in the documentation.

--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.
.



Relevant Pages

  • Re: Horrid Serial Dacs
    ... >Debugging is twice as hard as writing the code in the first place. ...
    (sci.electronics.design)
  • Re: Dear Odinn...
    ... Odinn posted ... That's pretty good, considering you DID reply to me, TWICE. ... After I had to hunt down your e-mail address in your whois info for ...
    (rec.woodworking)
  • Re: Banned
    ... tussock wrote: ... posted) twice. ... Passed along to my playing group. ...
    (rec.games.frp.dnd)
  • Re: Great SWT Program
    ... Explorer lets you type not only *just* the ... the name twice. ... criteria if they're all ones "find" can, um, find. ...
    (comp.lang.java.programmer)