help-octave
[Top][All Lists]
Advanced

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

Re: Increment on index expressions


From: Carlo de Falco
Subject: Re: Increment on index expressions
Date: Mon, 22 Jun 2009 18:21:25 +0200


On 22 Jun 2009, at 18:03, arriquitaun wrote:


Hi,

I'm using octave 3.0.1 under Ubuntu 9.04 (64-bit).

I'd like to increment certain elements of a vector whose indices are given
in another vector, but I get undesired results. Say

octave:50> a=zeros(1,3);
octave:51> b=[1 2 1 1 2 1];
octave:52> a(b)++;
octave:53> a
a =

  1   1   0

However, I would expect (i.e. I'd like) to obtain
a =

  4   2   0

as the index 1 is found 4 times in b, index 2 is found 2 times in b, and
index 3 is not in b.
Using pre-increment, i.e. ++a(b), produces the same result.

I'm now using an explicit loop (for) to do this, as follows:
octave:54> a=zeros(1,3);
octave:55> for i=[1:length(b)]
a(b(i))++;
endfor
octave:56> a
a =

  4   2   0

However, I'd like to get rid of it since I want to do this over very large (1e6 elements) vectors and in this case it takes a lot of time, whereas
simple operations over such large vectors are quite fast.

Is it possible to do what I intend in a fast way?

This topic has already been brought up very recently, have a look at this thread:

http://www.nabble.com/unexpected-behavior-of-%2B%3D-td23996294.html

c.



reply via email to

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