From bug-octave-request at bevo dot che dot wisc dot edu Fri Dec 12 20:30:19 2003 Subject: Re: more indexing weirdness From: Etienne Grossmann To: Paul Kienzle Cc: bug-octave at bevo dot che dot wisc dot edu, etienne@cs.uky.edu Date: Fri, 12 Dec 2003 21:30:03 -0500 Hi all, for this, you may want to use loop_add() below: octave:4> loop_add ([0 0 0],[2 2 2 2 1],[1 1 1 1 1]) ans = 1 4 0 Hth, Etienne ====================================================================== ## 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 code sent to ## octave-help (3 Aug 2000). ## Author : Etienne Grossmann 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); ====================================================================== On Thu, Dec 11, 2003 at 11:48:03PM -0500, Paul Kienzle wrote: # octave-2.1.52, Debian linux. # # Shouldn't the following add 5? # # octave:24> x=eye(2); x([1,1,1,1,1])+=1 # x = # 2 0 # 0 1 # # octave:25> x=[1:4]; x([1,1,1,1,1])+=1 # x = 2 2 3 4 # # Paul Kienzle # pkienzle at users dot sf dot net # # # # ------------------------------------------------------------- # 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 # ------------------------------------------------------------- # -- Etienne Grossmann ------ http://www.cs.uky.edu/~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 -------------------------------------------------------------