[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Indirect Addressing Question
From: 
John W. Eaton 
Subject: 
Indirect Addressing Question 
Date: 
Tue, 13 Nov 2001 19:16:56 0600 
On 13Nov2001, Hartmut Henkel <address@hidden> wrote:
 Please excuse the following newbie question:

 With

 a = [1 1 1 1 1]
 q = [1 1 1 2 2]

 the following

 a(q(1:5)) = a(q(1:5)) + 10

 gives

 a = 11 11 1 1 1
In Octave, the RHS is always evalutated first. So for the above
expression, you have
a(q(1:5)) + 10 ==> a([1, 1, 1, 2, 2]) + 10
[1, 1, 1, 1, 1] + 10
[11, 11, 11, 11, 11]
then the assignment is performed. For the above example, we have
a([1, 1, 1, 2, 2]) = [11, 11, 11, 11, 11]
and this expression says to assign
a(1) = 11
a(1) = 11
a(1) = 11
a(2) = 11
a(2) = 11
 But I would expect

 a = 31 21 1 1 1

 which I get by running the loop

 for i = 1 : 5
 a(q(i)) = a(q(i)) + 10
 endfor

 It seems that such indirect addressing is not allowed. Is there a trick
 to circumvent the forloop?
I'm not sure of a good way. Perhaps someone else knows a trick?
BTW, this question has come up before. Is there some other array
language that works like this?
jwe

Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
