How to avoid the FOR loop when using TM_TEST? [message #55497] |
Fri, 24 August 2007 06:43  |
wanglin1981
Messages: 10 Registered: August 2007
|
Junior Member |
|
|
I need to test the significance for each spatial point between two
data sets (names are A(ix,iy,it1) and B(ix,iy,it2), ix and iy are
spatial point, and it1 and it2 are temporal point) using the TM_TEST
function. As the TM_TEST is not valid for two-dimentional matrix, I
had to use the FOR loop as follows, which cost a lot of time.
for j=0,iy-1 do begin
for i=0,ix-1 do begin
temp1=tm_test(A(i,j,*),B(i,j,*))
sighl(i,j)=temp1(1)
endfor
endfor
Is it possible to avoid the FOR loops?
Thanks!
|
|
|
Re: How to avoid the FOR loop when using TM_TEST? [message #55545 is a reply to message #55497] |
Mon, 27 August 2007 07:07  |
wanglin1981
Messages: 10 Registered: August 2007
|
Junior Member |
|
|
On Aug 26, 12:26 am, Allan Whiteford <allan-remove-th...@-and-
this.phys-dot-strath.ac.uk> wrote:
> wanglin1...@gmail.com wrote:
>> I need to test the significance for each spatial point between two
>> data sets (names are A(ix,iy,it1) and B(ix,iy,it2), ix and iy are
>> spatial point, and it1 and it2 are temporal point) using the TM_TEST
>> function. As the TM_TEST is not valid for two-dimentional matrix, I
>> had to use the FOR loop as follows, which cost a lot of time.
>
>> for j=0,iy-1 do begin
>> for i=0,ix-1 do begin
>> temp1=tm_test(A(i,j,*),B(i,j,*))
>> sighl(i,j)=temp1(1)
>> endfor
>> endfor
>
>> Is it possible to avoid the FOR loops?
>
>> Thanks!
>
> Lin,
>
> Welcome to the group :).
>
> For the code you have, you can get a significant speed up if you replace
> it all (including the loops) with:
>
> na=(size(a,/dim))[2]
> nb=(size(b,/dim))[2]
> meanx=total(a,3) / na
> meany=total(b,3) / nb
> df = 1.0*(na+nb-2)
> t = (meanx-meany)/sqrt((((total((a-rebin(meanx,ix,iy,na))^2,3) + $
> total((b-rebin(meany,ix,iy,nb))^2,3) )/df) * (1.0/na + 1.0/nb)))
> sighl = ibeta(0.5*df,0.5,df/(df+t^2))
>
> give or take some line breaks which I'm sure have been broken posting it
> across a newsgroup.
>
> This will give you s sighl array identical to the one you get with your
> present code.
>
> However, unless the speed is a real issue then your present solution is
> much easier to read and maintain. Note also that this doesn't give any
> of the other options which tm_test takes.
>
> Good question; keep them coming.
>
> Thanks,
>
> Allan- Hide quoted text -
>
> - Show quoted text -
Allan,
Thank you very much. After using the code you provided, the speed is
significantly improved, and I got the exact same result as that using
TM_TEST. Your method is illuminating for me when I meet with similar
problems.
Also, I really appreciate your encouragement. Learning IDL is a
journal full of challenge and interest. I enjoy this journey very much
and will keep moving on.
Regards,
Lin
|
|
|
Re: How to avoid the FOR loop when using TM_TEST? [message #55565 is a reply to message #55497] |
Sat, 25 August 2007 09:26  |
Allan Whiteford
Messages: 117 Registered: June 2006
|
Senior Member |
|
|
wanglin1981@gmail.com wrote:
> I need to test the significance for each spatial point between two
> data sets (names are A(ix,iy,it1) and B(ix,iy,it2), ix and iy are
> spatial point, and it1 and it2 are temporal point) using the TM_TEST
> function. As the TM_TEST is not valid for two-dimentional matrix, I
> had to use the FOR loop as follows, which cost a lot of time.
>
> for j=0,iy-1 do begin
> for i=0,ix-1 do begin
> temp1=tm_test(A(i,j,*),B(i,j,*))
> sighl(i,j)=temp1(1)
> endfor
> endfor
>
> Is it possible to avoid the FOR loops?
>
> Thanks!
>
Lin,
Welcome to the group :).
For the code you have, you can get a significant speed up if you replace
it all (including the loops) with:
na=(size(a,/dim))[2]
nb=(size(b,/dim))[2]
meanx=total(a,3) / na
meany=total(b,3) / nb
df = 1.0*(na+nb-2)
t = (meanx-meany)/sqrt((((total((a-rebin(meanx,ix,iy,na))^2,3) + $
total((b-rebin(meany,ix,iy,nb))^2,3) )/df) * (1.0/na + 1.0/nb)))
sighl = ibeta(0.5*df,0.5,df/(df+t^2))
give or take some line breaks which I'm sure have been broken posting it
across a newsgroup.
This will give you s sighl array identical to the one you get with your
present code.
However, unless the speed is a real issue then your present solution is
much easier to read and maintain. Note also that this doesn't give any
of the other options which tm_test takes.
Good question; keep them coming.
Thanks,
Allan
|
|
|
Re: How to avoid the FOR loop when using TM_TEST? [message #55575 is a reply to message #55497] |
Sat, 25 August 2007 00:43  |
wanglin1981
Messages: 10 Registered: August 2007
|
Junior Member |
|
|
David,
Thank you for your reply. Luck the number of loops is not huge.
Regards,
Lin
> Sorry, Lin. Humor is the hardest thing to translate
> correctly. We welcome everyone here, and if my answer
> seemed disrespectful, I apologize.
>
> Your input function does not take vectors or arrays.
> Therefore, if you want to call it many times, you will
> have to do it in a loop or find another input function
> (or, possibly, you can change the one you have). But
> as you have presented the problem, there is no better solution
> than the one you found.
>
> Cheers,
>
> David
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.dfanning.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: How to avoid the FOR loop when using TM_TEST? [message #55580 is a reply to message #55497] |
Fri, 24 August 2007 21:46  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Lin writes:
> I'm not a native English speaker and I cannot quite understand
> what you mean in the reply. But I can sense that maybe you thought
> me as a bad learner.
Sorry, Lin. Humor is the hardest thing to translate
correctly. We welcome everyone here, and if my answer
seemed disrespectful, I apologize.
Your input function does not take vectors or arrays.
Therefore, if you want to call it many times, you will
have to do it in a loop or find another input function
(or, possibly, you can change the one you have). But
as you have presented the problem, there is no better solution
than the one you found.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|