From help-request at octave dot org Fri Apr 14 09:52:25 2006 Subject: Re: how do I vectorize this? From: Etienne Grossmann To: help at octave dot org Cc: etienne at cs dot uky dot edu Date: Fri, 14 Apr 2006 10:48:16 -0400 --sm4nu43k4a2Rpi4c Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Hi, sthing like this? octave:2> help loop_add loop_add is the dynamically-linked function from the file /homeætienne/progøctave/miscøctave-3Dvision/misc/loop_add.oct -- Loadable Function: B = loop_add (A, INDICES, WHAT) This function is equivalent to, but is quicker than : B=A; for J=1:rows(INDICES(:)), B(INDICES(J)) += WHAT(J); end WHAT is optional and is 1 by default. If WHAT has less elements than INDICES, values of WHAT are wrapped. I.e. WHAT(1 + rem(J-1,rows(WHAT(:))) is used instead of WHAT(J). octave:11> loop_add (zeros(1,10),(1:3).^2) ans = 1 0 0 1 0 0 0 0 1 0 octave:12> loop_add (zeros(1,10),(1:3).^2,2*ones(1,3)) ans = 2 0 0 2 0 0 0 0 2 0 octave:13> loop_add (zeros(1,10),(1:3).^2,[2 3]) ans = 2 0 0 3 0 0 0 0 2 0 octave:14> loop_add (zeros(1,10),(1:4).^2,[2 3]) error: loop_add: Can't add to a(16) because a has only 10 elements. If yes, it's in the attachment. Hth, Etienne On Fri, Apr 14, 2006 at 12:56:43PM +0200, Miroslaw Kwasniak wrote: # On Fri, Apr 14, 2006 at 10:52:18AM +0200, Claudio Belotti wrote: # > # > Hi Brendan, # > thank you for your reply. This is not really what I need as my data are ungridded I don't think it would be convinient to organize them in a matrix. # # Look in archives for an idea in thread "for i=1:bignum, x(a(i)) += y(i); # end" from 2005. # # PS # # I think octave archives are incomplete, I'm sure that were more then two # posts in this thread :( # # # # ------------------------------------------------------------- # 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 --sm4nu43k4a2Rpi4c Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="loop_add.cc" #include #include #include #include //using namespace std; // Author : Etienne Grossmann , 2005 DEFUN_DLD (loop_add, args, ,\ "-*- texinfo -*-\n\ at deftypefn {Loadable Function} {@var{b} =} loop_add (@var{a}, @var{indices}, @var{what})\n\ \n\ This function is equivalent to, but is quicker than :\n\ \n\ at var{b}=@var{a}; for @var{j}=1:rows(@var{indices}(:)), @var{b}(@var{indices}(@var{j})) += @var{what}(@var{j}); end\n\ \n\ at var{what} is optional and is 1 by default.\n\ \n\ If at var{what} has less elements than @var{indices}, values of @var{what} are wrapped. I.e. @var{what}(1 + rem(@var{j}-1,rows(@var{what}(:))) is used instead of @var{what}(@var{j}).\n\ at end deftypefn") { // octave_value_list retval; if (args.length()<2){ error ("loop_add: called w/ %i argument(s); 2-3 are needed.",args.length()); return octave_value (); } Matrix res(args(0).matrix_value()); Matrix inds(args(1).matrix_value()); //Matrix what(args(2).matrix_value()); Matrix what(1,1,1.0); if (args.length()>2) { what = args(2).matrix_value(); } int norig = res.columns() * res.rows() - 1; int ninds = inds.columns() * inds.rows(); int nwhat = what.columns() * what.rows(); //printf ("Size of A:%i, of INDICES:%i, of WHAT:%i\n",norig+1,ninds,nwhat); for (int i=0; i norig)){ // Check index if (j<0) error ("loop_add: Can't add to a(%i) : index is not positive.",j+1); else error ("loop_add: Can't add to a(%i) because a has only %i elements.",j+1,norig+1); return octave_value (); } res(j) = res(j) + what(i % nwhat); } return octave_value (res); } --sm4nu43k4a2Rpi4c-- ------------------------------------------------------------- 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 -------------------------------------------------------------