[Index for tmp_for_tar/misc] [Return to Master Index]

loop_add

(tmp_for_tar/misc/loop_add.m)


Function Synopsis

c = loop_add (a, i, b)

Help text

 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 : Any size   : Indices, should have values in 1:R*C.
 b  : Same number of elements as ii, or 1. Added value(s). If b is
                   missing, a value of 1 is assumed.

 c  : R x C    

 Inspired by Dirk Laurie's <dirk@calvyn.puk.ac.za> code sent to
 octave-help (3 Aug 2000).

 Last modified: April 2001



Listing of function file tmp_for_tar/misc/loop_add.m

## 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 : Any size   : Indices, should have values in 1:R*C.
## b  : Same number of elements as ii, or 1. Added value(s). If b is
##                   missing, a value of 1 is assumed.
##
## c  : R x C    
##
## Inspired by Dirk Laurie's <dirk@calvyn.puk.ac.za> code sent to
## octave-help (3 Aug 2000).


## Author:        Etienne Grossmann  <etienne@isr.ist.utl.pt>
## Last modified: April 2001

function c = loop_add (a, i, b)

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

c = a(:);
[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);



Produced by oct2html on Sat Apr 28 21:14:54 2001
Cross-Directory links are: ON