Re: Referencing elements of array expressions [message #3513] |
Mon, 06 February 1995 08:18 |
velt
Messages: 19 Registered: June 1994
|
Junior Member |
|
|
In article <3guag9$nqn@marsupial.jpl.nasa.gov>, "Evan F. Fishbein" <eff@tugh.jpl.nasa.gov> writes:
> A simple question -- Is is possible to reference elements
> of array expressions. For example, can the last two lines of
> the following code:
>
> x=findgen(40)/10.0
> y=cos(2.0*!pi*x)
> i=where(abs(y) lt 1.0e-5)
> i=i(0:1)+[-1,1]
>
> be combined into one line by accessing the first
> element of the expression "where(abs(y) lt 1.0e-5)"
> without placing the vector result in a dumy array variable?
This is a simple trick-question, isn't it? The where finds no elements
satisfying the condition, so the last statement will generate an error.
However, in general you can index expressions by surrounding it
with parentheses and indexing that:
IDL> result = (expression) (elements)
For example, the last two lines above can be written:
IDL> i=( where(abs(y) lt 1.0E-5) ) (0:1) + [-1,1]
Robert Velthuizen (velt@rad.usf.edu),
Digital Medical Imaging Program of the
H. Lee Moffitt Cancer Center and Research Institute at the
University of South Florida, Tampa FL 33612.
|
|
|
Re: Referencing elements of array expressions [message #3521 is a reply to message #3513] |
Sun, 05 February 1995 18:10  |
steinhh
Messages: 260 Registered: June 1994
|
Senior Member |
|
|
In article <3guag9$nqn@marsupial.jpl.nasa.gov>, "Evan F. Fishbein" <eff@tugh.jpl.nasa.gov> writes:
|>
|> A simple question -- Is is possible to reference elements
|> of array expressions. For example, can the last two lines of
|> the following code:
|>
|> x=findgen(40)/10.0
|> y=cos(2.0*!pi*x)
|> i=where(abs(y) lt 1.0e-5)
|> i=i(0:1)+[-1,1]
|>
|> be combined into one line by accessing the first
|> element of the expression "where(abs(y) lt 1.0e-5)"
|> without placing the vector result in a dumy array variable?
|>
Yes, it's possible:
i = (where(abs(y) lt 1.0e-5))(0:1)+[-1,1]
and in general, you can reference elements of arrays in expressions
by adding parentheses around the array expression, and then the index
in another pair of parentheses:
sub_array = (my_arrayfunc(0.5))(0:5)
I wouldn't recommend it in your example, though, because WHERE might
return a scalar long if no elements match the test, but I often use
it with e.g. N_DIMENSIONS = (SIZE(a))(0)
Stein Vidar
|
|
|