|
Re: efficient summation: msg#00005python.numeric.general
Em Qua, 2004-09-01 às 18:51, Darren Dale escreveu: > I am trying to effieciently sum over a subset of the elements of a > matrix. In Matlab, this could be done like: > a=[1,2,3,4,5,6,7,8,9,10] > b = [1,0,0,0,0,0,0,0,0,1] > res=sum(a(b)) %this sums the elements of a which have corresponding > elements in b that are true If the mask is of boolean type (not integer) you can use it just like in MATLAB: >>> from numarray import * >>> import numarray.random_array as ra >>> a = ra.random(1000000) >>> sum(a) 500184.16988508566 >>> b = ra.random(1000000) < 0.1 >>> sum(a[b]) 50331.373006955822 This should work for numarray only. Paulo -- Paulo José da Silva e Silva Professor Assistente do Dep. de Ciência da Computação (Assistant Professor of the Computer Science Dept.) Universidade de São Paulo - Brazil e-mail: rsilva@xxxxxxxxxx Web: http://www.ime.usp.br/~rsilva Teoria é o que não entendemos o (Theory is something we don't) suficiente para chamar de prática. (understand well enough to call practice) ------------------------------------------------------- This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools! Get your free copy of BEA WebLogic Workshop 8.1 today. http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: efficient summation: 00005, Stephen Walton |
|---|---|
| Next by Date: | Re: efficient summation: 00005, Darren Dale |
| Previous by Thread: | Re: efficient summationi: 00005, Darren Dale |
| Next by Thread: | Re: extracting a random subset of a vector: 00005, Curzio Basso |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |