Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
Jonas wrote: > > thanxalot (both Kevin and David), it helped. I wasn't avare of the function > "TOTAL", still being a newbie and all. > > This was kind of a special example since total could do all the work. What > if I would like to do another calculation where total is not applicable... > in other words: > is there generally an easy way to perform the same calculation over all the > (sub-)rows in a in a 2D array? > > I do not have such a problem at the moment, but I assume it is likely that I > will encounter it in the future (at least if i don't ask now :-)). > > sincerely > Jonas Svensson Well, if you want to compute averages first over rows 0:3, then 4:7, then 8:11, etc. you may not get around using a loop. Although, in some cases you could try your luck with reform() and a subsequent call to Kevin's average routine (which I like very much: THANKS!). But this is certainly prone to errors... Martin. -- ------------------------------------------------------------ ------- Dr. Martin Schultz Department for Earth&Planetary Sciences, Harvard University 109 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA phone: (617)-496-8318 fax : (617)-495-4551 e-mail: mgs@io.harvard.edu Internet-homepage: http://www-as.harvard.edu/people/staff/mgs/ ------------------------------------------------------------ -------
thanxalot (both Kevin and David), it helped. I wasn't avare of the function "TOTAL", still being a newbie and all. This was kind of a special example since total could do all the work. What if I would like to do another calculation where total is not applicable... in other words: is there generally an easy way to perform the same calculation over all the (sub-)rows in a in a 2D array? I do not have such a problem at the moment, but I assume it is likely that I will encounter it in the future (at least if i don't ask now :-)). sincerely Jonas Svensson
Jonas wrote: > I want to perform the same operation on each sub-row in a 2D array. > Say I want to calculate the mean of element 4-7 in each row of a 10x10 > array, and store the result in a 10 element-vector, where each element holds > the mean from the respective row > > how is this done the smartest way, without using time-consuming loops? A general anwser is not easy, but your example is. You need routines that only work on certain dimensions of multidimensional matrices. For calculating the mean I have exactly what you need: It is a one-liner (with a few comments) named 'average'. Here is an example: IDL> ten_2d = bindgen(10,10) IDL> print, average(ten_2d(4:7,*), 1) 5.50000 15.5000 25.5000 35.5000 45.5000 55.5000 65.5000 75.5000 85.5000 95.5000 Hope this helps, Kevin -- Kevin Ivory Tel: +49 5556 979 434 Max-Planck-Institut fuer Aeronomie Fax: +49 5556 979 240 Max-Planck-Str. 2 mailto:Kevin.Ivory@linmpi.mpg.de D-37191 Katlenburg-Lindau, GERMANY http://www.gwdg.de/~kivory2/ ------------------------------------------------------------ ------------------ ; Time-stamp: <average.pro Thu Apr 3 16:00:48 MET DST 1997> function average, array, dim, _extra=_extra ;+ ; calculates the average value of an array (all arguments as in 'total') ; arguments ; array array to be averaged, any type except string ; dim dimension over which to average (see 'total' documentation) ; keywords ; _extra all keywords passed to 'total' ;- if n_elements(dim) eq 0 then dim = 0 return, total(array, dim, _extra=_extra) / (total(finite(array), dim)>1) end
"Jonas" <jonas_2@hotmail.com> writes: > Probably a damn simple one, but anyway: > > I want to perform the same operation on each sub-row in a 2D array. > Say I want to calculate the mean of element 4-7 in each row of a 10x10 > array, and store the result in a 10 element-vector, where each element holds > the mean from the respective row > > how is this done the smartest way, without using time-consuming loops? mean = total(array[4:7,*],1)/4 -- David Kastrup Phone: +49-234-700-5570 Email: dak@neuroinformatik.ruhr-uni-bochum.de Fax: +49-234-709-4209 Institut fï¿½r Neuroinformatik, Universitï¿½tsstr. 150, 44780 Bochum, Germany
Jonas wrote: > This was kind of a special example since total could do all the work. What > if I would like to do another calculation where total is not applicable... > in other words: > is there generally an easy way to perform the same calculation over all the > (sub-)rows in a in a 2D array? Calculating the average of each row could be done by matrix multiplication: a = findgen( 10, 10 ) b = reform( fltarr( 10 ) + 1, 1, 10 ) average = reform( b # a / 10 ) print, average 4.50000 14.5000 24.5000 34.5000 44.5000 54.5000 64.5000 74.5000 84.5000 94.5000 Other things would be more difficult, though, and sometimes you just cannot avoid loops. Alex -- Alex Schuster Wonko@weird.cologne.de PGP Key available alex@pet.mpin-koeln.mpg.de
Martin Schultz skrev i meddelandet <35E326AF.41C6@io.harvard.edu>... > Well, if you want to compute averages first over rows 0:3, then 4:7, > then 8:11, etc. you may not get around using a loop. Although, in some > cases you could try your luck with reform() and a subsequent call to > Kevin's average routine (which I like very much: THANKS!). But this is > certainly prone to errors... > > Martin. > > What I meant was, doing all the calculations on the same sub-row in each row. The question then is: Is there a general way to access all these subrows if the calculation to be performed is not including "TOTAL". Jonas