help-octave
[Top][All Lists]
Advanced

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

Re: a=0; a([1,1])++ -> a == 2


From: Dirk Laurie
Subject: Re: a=0; a([1,1])++ -> a == 2
Date: Thu, 3 Aug 2000 08:46:55 +0200

Etienne Grossmann skryf:
> 
>   Hello,
> 
>   sorry if the answer to my question is a clear-cut "no" or if it has
> already been discussed.
> 
>   Wouldn't it make sense to have the code 
> 
>        a=0; b = a([1,1])++ ;
> 
>   be equivalent to either
> 
>        a=0; b=a([1,1]); b++; for i in [1,1], a(i)++ ; end
> 
>        yielding       a == 2, b == [1;1]
>   or 
>        a=0; b=zeros(size(a)); for i in [1,1], b(i) = a(i)++ ; end
>     
>        yielding       a == 2, b == [1;2]
> 
The ++ operator is inherited from C, and it would be highly confusing,
to say the least, if the behaviour should be different from that of C.
So the code

  a=0; b = a([1,1])++ ;

should be equivalent to

  a=0; b=a([1,1]); a([1,1])=a([1,1])+1;

whereas 

  a=0; b = ++a([1,1]) ;

should be equivalent to 

  a=0; a([1,1])=a([1,1])+1; b=a([1,1]);

Therefore in the first case, an experienced C programmer would expect

  a == 1; b == [0;0];

and in the second
 
  a == 1; b == [1;1];

This is indeed what Octave 2.1.30 delivers.

If you need to vectorize

  for k in I, a(k)++; end

to work also when there are repeated indices, you could use

  J=sort(I);
  k=find([1,diff(J)]);
  a(J(k))=diff([k,length(J)+1]); 

Dirk



-----------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.che.wisc.edu/octave/octave.html
How to fund new projects:  http://www.che.wisc.edu/octave/funding.html
Subscription information:  http://www.che.wisc.edu/octave/archive.html
-----------------------------------------------------------------------



reply via email to

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