From bug-octave-request at bevo dot che dot wisc dot edu Tue Jan 7 08:34:45 2003 Subject: Re: Operations on matrix referenced by a list of indices From: Etienne Grossmann To: Igor ?ukanovi? Cc: bug-octave at bevo dot che dot wisc dot edu, Etienne Grossmann Date: Tue, 7 Jan 2003 14:47:57 +0000 Hello, maybe the following function can help you; you may want to make it more efficient : ## 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); endfunction Hth, Etienne On Sun, Jan 05, 2003 at 05:48:59PM -0600, Igor ?ukanovi? wrote: # Let A be a vector or a matrix. (e.g. A=[ 1 2 3 4 5]). # Statement # A([ 1 2]) = [ 7 8]; # does # A(1)=7; A(2)=8; # and that's good. # Statement # A([1 1])= [ 8 9]; # does # A(1) = 9; # and that's good, too. # # However statement # A([ 1 1])= A([1 1]) + [ 8 9]; # does (same as in Matlab) # A(1) = A(1) + 9; # and that's bad. I want it to do # A(1) = A(1) + 8; A(1)=A(1) + 9; # producing # A(1)=A(1)+8+9; # otherwise I would not include 1 in index list twice. # Is my wish in conflict with other functions written in octave? # # Best regards, # igor dukanovic # # P.S. Info: octave-2.1.36 # Windows 98 on Pentium -- 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 -------------------------------------------------------------