help-octave
[Top][All Lists]
Advanced

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

Indirect Addressing Question


From: Etienne Grossmann
Subject: Indirect Addressing Question
Date: Wed, 14 Nov 2001 09:46:21 +0000

  Hello,

  you can do that w/ the loop_add() function below :

======================================================================
## c = loop_add (a, ii, b=1)    - Add b to a at subscripts specified by ii
##
## This function is equivalent to, but should be quicker than :
##
##    c=a; for j=1:rows(ii(:)), c(ii(j)) += b(j); end
##
## a  : R x C  : Original matrix
## ii : S x D  : Indices in a. Values not in 1:(R*C) are ignored.
## b  : S x D  : Added value(s). If b is missing, b=1 is assumed.
##  or  1 x 1                    
##
## c  : R x C    
##
## Inspired by Dirk Laurie's <address@hidden> code sent to
## octave-help (3 Aug 2000).

function c = loop_add (a, i, b)

if isempty (i), c = a; return; end

if nargin < 3,
  b = ones (size (i));
elseif prod (size (b))==1,
  b = b * ones (size (i));
end
sz = size (a);

c = a(:);
i = i(find (i>0 & i<=prod(sz)));
[i,ii] = sort (i(:));
b = b(ii)(:);                   # The values in the order I'll add them

                                # The indices that interest me
jj = [find (diff (i)); rows (b)];

c(i(jj)) += diff ([0; cumsum (b)(jj)]);

c = reshape (c, sz);
======================================================================

  I have put that function in the "misc" package on my octave page
(http://www.isr.ist.utl.pt/~etienne/octave). 

  Hth,

  Etienne


From: Hartmut Henkel <address@hidden>

# 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
#
# 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 for-loop?





-- 
Etienne Grossmann ------ http://www.isr.ist.utl.pt/~etienne



-------------------------------------------------------------
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
-------------------------------------------------------------



reply via email to

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