Re: Help: how to do ? [message #15269] |
Tue, 04 May 1999 00:00 |
eddie haskell
Messages: 29 Registered: September 1998
|
Junior Member |
|
|
"Robert S. Hill" wrote:
> I added the following method to Pavel's test program:
>
> a1=findgen(1,3)
> a2=findgen(n,3)
> a3 = make_array(n,val=1) # a1
> a4 = a2 + a3
For what it's worth, I find that, at least on my system, replicate
works about 10-20% faster than make_array, as in the following:
a1 = findgen(1,3)
a2 = findgen(n,3)
a3 = a2 + (replicate(1,n) # a1)
Cheers,
eddie
----- ---- --- --- ---- --- -- --- --- -- -- - - - -
|\ A G Edward Haskell
|\ Center for Coastal Physical Oceanography
|\ Old Dominion University, Norfolk VA 23529
|\ Voice 757.683.4816 Fax 757.683.5550
|\ e-mail haskell*ccpo.odu.edu
----- ---- --- ---- --- --- --- --- -- -- -- - - - -
|
|
|
Re: Help: how to do ? [message #15273 is a reply to message #15269] |
Tue, 04 May 1999 00:00  |
Pavel Romashkin
Messages: 166 Registered: April 1999
|
Senior Member |
|
|
Tri VU KHAC wrote:
> Hi again,
> I post this message, but I think I have a better solution ;-)
> Just:
> A[*, 0] = A1[*, 0] + A2[0]
> A[*, 1] = A1[*, 1] + A2[1]
> A[*, 2] = A1[*, 2] + A2[2]
> Why did I post this message ? Because it does not look very nice to
> write.
You could even go without A1 at all, like
A[*, 0] = A2[0] + 0.0
A[*, 1] = A2[1] + 1.0
A[*, 2] = A2[2] + 2.0
if you weren't going to use this in a more general way than you
describe. Eliminate the first line as it does insignificant changes, and
the code will be even nicer to write. You may find this way inconvenient
if you go to more than 500 rows though, as the code won't fit on the
screen. Why did I post this message? Because I think that previous two
solutions looked nicer to write.
Cheers,
Pavel
|
|
|
Re: Help: how to do ? [message #15275 is a reply to message #15269] |
Tue, 04 May 1999 00:00  |
VU KHAC Tri
Messages: 25 Registered: March 1999
|
Junior Member |
|
|
Hi again,
I post this message, but I think I have a better solution ;-)
Just:
A[*, 0] = A1[*, 0] + A2[0]
A[*, 1] = A1[*, 1] + A2[1]
A[*, 2] = A1[*, 2] + A2[2]
Why did I post this message ? Because it does not look very nice to
write.
Best regards,
Tri.
> Hi folks,
>
> Having 2 arrays:
> + 3x1 : A1 = findgen(1, 3)
> + 3xN: A2= findgen(N, 3)
>
> No I need to do sth like:
>
> for i=1, N do A[i,*] = A2[i,*] + A1
>
> How to avoid FOR-DO here ?
>
> Thanks for your help.
> Regards,
> Tri.
|
|
|
Re: Help: how to do ? [message #15277 is a reply to message #15269] |
Tue, 04 May 1999 00:00  |
Robert S. Hill
Messages: 11 Registered: January 1998
|
Junior Member |
|
|
On Tue, 4 May 1999, Pavel Romashkin wrote:
> Hi Tri,
> Now I tested it better. Try the following:
>
I added the following method to Pavel's test program:
start=systime(1)
a1=findgen(1,3)
a2=findgen(n,3)
a3 = make_array(n,val=1) # a1
a4 = a2 + a3
print, systime(1) - start
print, a4
I can't remember where I ran across this trick of using a matrix multiply
operation to replicate rows or columns. In any case, it's about the same
speed as Pavel's method (using the reform function) for small values of N,
but on my system takes about 0.6 the time of Pavel's method for n=100000
or more. How it depends on the Y dimension, I haven't checked.
Bob Hill
--
Robert.S.Hill.1@gsfc.nasa.gov Phone: 301-286-3624
Raytheon ITSS / Code 681, NASA/GSFC, Greenbelt, MD 20771
|
|
|
Re: Help: how to do ? [message #15278 is a reply to message #15269] |
Tue, 04 May 1999 00:00  |
Pavel Romashkin
Messages: 166 Registered: April 1999
|
Senior Member |
|
|
Hi Tri,
Now I tested it better. Try the following:
pro test, N
start=systime(1)
a1=findgen(1,3)
a2=findgen(N, 3)
a3=a2
for i=0,N-1 do a3[i, *] = a2[i, *]+a1
print, systime(1) - start
print, a3
start=systime(1)
a1 = fix(findgen(N*3)/N)
a2 = findgen(N*3)
a3 = reform((a2+a1), N, 3)
print, systime(1) - start
print, a3
end
Still is faster by a factor of 10.
Cheers,
Pavel
Tri VU KHAC wrote:
> Having 2 arrays:
> + 3x1 : A1 = findgen(1, 3)
> + 3xN: A2= findgen(N, 3)
>
> No I need to do sth like:
>
> for i=1, N do A[i,*] = A2[i,*] + A1
>
> How to avoid FOR-DO here ?
|
|
|
Re: Help: how to do ? [message #15279 is a reply to message #15269] |
Tue, 04 May 1999 00:00  |
Pavel Romashkin
Messages: 166 Registered: April 1999
|
Senior Member |
|
|
Tri,
I was wrong. I did not replicate A1 into all columns and filled it with
zeros instead. The result you need can not be obtained this way. I am
sorry.
Cheers,
Pavel
Pavel Romashkin wrote:
> a1 = [[a1], fltarr(n-1, 3)]
> a = a2+a1
|
|
|
Re: Help: how to do ? [message #15281 is a reply to message #15269] |
Tue, 04 May 1999 00:00  |
Pavel Romashkin
Messages: 166 Registered: April 1999
|
Senior Member |
|
|
Tri VU KHAC wrote:
> Hi folks,
>
> Having 2 arrays:
> + 3x1 : A1 = findgen(1, 3)
> + 3xN: A2= findgen(N, 3)
>
> No I need to do sth like:
>
> for i=1, N do A[i,*] = A2[i,*] + A1
>
> How to avoid FOR-DO here ?
a1 = [[a1], fltarr(n-1, 3)]
a = a2+a1
I clocked the (100x1000) array. This method is 10 times faster than a
loop.
Cheers,
Pavel
|
|
|