row calculation in a 2D array [message #12604] |
Tue, 25 August 1998 00:00  |
Jonas
Messages: 23 Registered: May 1998
|
Junior Member |
|
|
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?
sincerely
Jonas
|
|
|
Re: row calculation in a 2D array [message #12702 is a reply to message #12604] |
Mon, 31 August 1998 00:00  |
Richard G. French
Messages: 65 Registered: June 1997
|
Member |
|
|
I don't know what the execution time penalty is for doing this,
but I have often used
avg=reform(rebin(A[3:6,*],1,10))
not sure if the reform is essential in this example but in general
it is useful for making sure a vector is a one-D array.
Dick French
>>
>> 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?
>>
>
|
|
|
Re: row calculation in a 2D array [message #12703 is a reply to message #12604] |
Mon, 31 August 1998 00:00  |
Vap User
Messages: 31 Registered: April 1998
|
Member |
|
|
"Jonas" <jonas_2@hotmail.com> writes:
The general case may be hard, if not impossible. Depends on what you
mean by 'operation' below. To do it generally probably requires that
the operation be a linear combination of the elements of the
arrays. Anyway, this will work to find the average.
if A is your 10 by 10 array.
tmpA=A[3:6,*] ; the sub-array
avg=replicate( 1, 4)##transpose(tmpA)/4.; the average.
or, equivalently, but transposed,
avg = transpose(replicate(1,4))#tmpA/4.
You should almost always be able to replace loops of summations with the
'#' (or '##') operator and a judicicious choice of multiplicand.
whd
>
> 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?
>
> sincerely
> Jonas
>
>
--
I don't speak for JPL, it doesn't speak for me.
Well, not all the time, at least.
William Daffer <vapuser@haifung.jpl.nasa.gov>
|
|
|