Re: Array operation question [message #33932 is a reply to message #33930] |
Fri, 07 February 2003 06:29  |
K. Bowman
Messages: 330 Registered: May 2000
|
Senior Member |
|
|
In article <b207km$enf$1@news.ox.ac.uk>,
Edd Edmondson <eddedmondson@hotmail.com> wrote:
> It's yet another question about how to get an efficient operation on an
> array:
>
> I have one array
> q=[num1,num2,num3,num4]
> and an array
> r=[ [num1a,num1b,num1c...],[num2a,num2b..],[num3a...],[num4a..] ]
> and I want to find w=r-q such that
> w=[ [num1a-num1,num1b-num1,num1c-num1...],[num2a-num2..],[num3a- num3..]..]
>
> Is there an efficient way of doing it without expanding q so that it is
> the same dimension as r? That'd be very expensive in terms of memory for
> me, unfortunately. I could loop over the 4 elements of q and r and do that
> seperately but I'd quite like to eliminate that last loop.
>
> I've tried various things but all fall victim to the 'feature' mentioned
> earlier that IDL will make the result have the dimensions of the smaller
> array.
The first rule of thumb of optimization is "optimize the innermost
loop", so
FOR j = 0, nj-1 DO w[0,j] = r[*,j]-q[j]
will be pretty efficient if the first dimension or r is large. (Note
that the zero on the lhs is important for efficiency.)
Ken Bowman
|
|
|