| Re: Help assigning values to ranges of elements in an array??? [message #5187] |
Thu, 12 October 1995 00:00 |
peter
Messages: 80 Registered: February 1994
|
Member |
|
|
Julian Marshall (jgm@nntp.unsw.EDU.AU) wrote:
: Hi.
: I was hoping someone could help me by suggesting a way by which
: I can assign values to a range of elements in an array. To be
: more exact, I want to make elements 0 to 100 (for eg.) of a
: 200 element array equal to a value or equal to values in a 100
: element array.
: Of course I can use for loops but I feel this is inefficient and
: a bit annoying when handling 256*256*256 bytarrs.
: IDL allows one to use an array operation to specify a range of
: values in an array (ie. A=B>10) but is it possible to use an
: array operation to specify a range of elements.
: Thanx
: julian
: jgm@newt.phys.unsw.edu.au
: .
:
You can specify a subarray on the LH side of an equation, so, for
example,
A(0:10) = 8
or
A(0:10) = B(10:20)
are legal, provided A is already allocated and big enough. For
assignment to more complicated shapes within A, you can collect all the
desired subscripts into another array, say, C, and use that to index A
A(C) = 0
Have a look at the chapter on array subscripting in the IDL manual, it
really is quite powerful.
Peter
--
--------------------------------
Peter Webb, HP Labs Medical Dept
E-Mail: peter_webb@hpl.hp.com
Phone: (415) 813-3756
|
|
|
|
|
|
| Re: Help assigning values to ranges of elements in an array??? [message #5199 is a reply to message #5196] |
Wed, 11 October 1995 00:00  |
nmw
Messages: 18 Registered: January 1995
|
Junior Member |
|
|
In article <45ff2r$n3f@mirv.unsw.edu.au>,
jgm@nntp.unsw.EDU.AU (Julian Marshall) writes:
> Hi.
> I was hoping someone could help me by suggesting a way by which
> I can assign values to a range of elements in an array. To be
> more exact, I want to make elements 0 to 100 (for eg.) of a
> 200 element array equal to a value or equal to values in a 100
> element array.
> Of course I can use for loops but I feel this is inefficient and
> a bit annoying when handling 256*256*256 bytarrs.
> IDL allows one to use an array operation to specify a range of
> values in an array (ie. A=B>10) but is it possible to use an
> array operation to specify a range of elements.
> Thanx
>
> julian
> jgm@newt.phys.unsw.edu.au
> ..
>
For your example you could simply do this (scaled down by 10 of course) :
IDL> a=indgen(20)
IDL> print,a
0 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19
IDL> b=intarr(10)+999
IDL> a(0:9)=23
IDL> print,a
23 23 23 23 23 23 23 23 23 23 10
11 12 13 14 15 16 17 18 19
IDL> a(0:9)=b
IDL> print,a
999 999 999 999 999 999 999 999 999 999 10
11 12 13 14 15 16 17 18 19
IDL>
You can also do things like:
IDL> a=indgen(5,3)
IDL> print,a
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
IDL> a(3,*)=99
IDL> print,a
0 1 2 99 4
5 6 7 99 9
10 11 12 99 14
IDL> a(2:4,1)=0
IDL> print,a
0 1 2 99 4
5 6 0 0 0
10 11 12 99 14
IDL>
Or if you had something a bit more complicated in mind :
IDL> a=indgen(5,3)
IDL> b=[0,2,4]
IDL> a(b,1)=999
IDL> print,a
0 1 2 3 4
999 6 999 8 999
10 11 12 13 14
IDL>
--
Nigel Wade, System Administrator, Ionospheric Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523568, Fax : +44 (0)116 2523555
|
|
|
|
| Re: Help assigning values to ranges of elements in an array??? [message #5200 is a reply to message #5196] |
Wed, 11 October 1995 00:00  |
rmmoss
Messages: 12 Registered: August 1995
|
Junior Member |
|
|
Use array indexing. For example
a = intarr(200)
a(0:100)=3
Will set the values for a(0) through a(100) equal to 3. You can use an
asterisk to specify an open range, for example
a( 100:*)=5
will set all the values of a(100) to a(199) to 5.
----------
Robert Moss, Ph.D.
Texaco Inc.
mossrm@texaco.com
This is not necessarily the opinion of Texaco Inc.
----------
In article <45ff2r$n3f@mirv.unsw.edu.au>, jgm@nntp.unsw.EDU.AU (Julian Marshall) writes:
|> Hi.
|> I was hoping someone could help me by suggesting a way by which
|> I can assign values to a range of elements in an array. To be
|> more exact, I want to make elements 0 to 100 (for eg.) of a
|> 200 element array equal to a value or equal to values in a 100
|> element array.
|> Of course I can use for loops but I feel this is inefficient and
|> a bit annoying when handling 256*256*256 bytarrs.
|> IDL allows one to use an array operation to specify a range of
|> values in an array (ie. A=B>10) but is it possible to use an
|> array operation to specify a range of elements.
|> Thanx
|>
|> julian
|> jgm@newt.phys.unsw.edu.au
|> .
|>
|
|
|
|