Re: Please help me avoid loops and conditionals [message #36331] |
Tue, 09 September 2003 12:30  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
pford@bcm.tmc.edu (Patrick Ford) writes:
> function elp2, a, b, box_dim, vval, e_a,e_b, I_ratio
> x_box = box_dim/2
> box = intarr(box_dim,box_dim)
> o_val = fix(vval / I_ratio)
> v = fix(vval)
> for i = 0, box_dim-1 do begin
> for j = 0, box_dim-1 do begin
> x = float(i - x_box)
> y = float(j - x_box)
> if( ((x/(a+e_a))^2 + (y/(b+e_b))^2) LE 1.0) then $
> if( ((x/a)^2 + (y/b)^2) LE 1.0) then box(i,j) = o_val $
> else box(i,j) = v
> endfor; j = 0, box_dim-1 do
> endfor; i = 0, box_dim-1 do
> return, box
> end
>
>
> So how do I go about converting this into a Boolean matrix operation
> that avoids all of this? Would it be faster to create a mask array
> such as:
>
> x = float((indgen(box_dim) � box_dim/2) # replicate(1, box_dim))
> y = (transpose(x) / b)^2
It's close to what I would do.
I would start by creating the X and Y arrays more or less as you have
done:
x = (fltarr(box_dim)+1) ## findgen(box_dim)
y = findgen(box_dim) ## (fltarr(box_dim)+1)
and then initializing the array to zeroes:
box = intarr(box_dim,box_dim)
then as you said, use the WHERE statement to pull out the values of
interest.
wh = where( (x/(a+e_a))^2 + (y/(b+e_b))^2 LE 1.0, ct)
... and fill them in. You appear to have a two stage process.
if ct GT 0 then begin
box(wh) = o_val
wh1 = where( (x(wh)/a)^2 + (y(wh)/b)^2 LE 1.0, ct1)
if ct1 GT 0 then box(wh(wh1)) = v
endif
Just as you, I didn't test this or nuthin'.
Good luck,
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|