Re: array subscripting problem [message #43416] |
Mon, 11 April 2005 08:11 |
Benjamin Luethi
Messages: 22 Registered: December 2004
|
Junior Member |
|
|
Hi Martin,
You could also extract a 3x3 sub-array first and then do the comparisons
on it. This way you don't need any if checks at all:
s = Size(data, /Dimensions)
subarray=data[ ((i-1)>0):((i+1)<(s[0]-1)), ((j-1)>0):((j+1)<(s[1]-1)), k ]
f=f+total(ABS(cd-subarray) LE mg)-1
Notice that the subarray is not 3x3 if i or j are at the border.
Also notice the -1 in the third line: it's because the [i,j]-element is
always part of the subarray and counts as well.
Ben
On 11 Apr 2005 06:00:14 -0700, Martin Doyle <m.doyle@uea.ac.uk> wrote:
> Hi Guys,
>
> I've got a gridded dataset and I'm searching the grid trying to find
> those points which have a certain value. Once I've found this point, I
> want to check all points around it to see if they have the same value
> plus a little bit. This is what I've come up with:
>
> If (data(i,j,k) GE p) THEN begin
>
> cd = data(i,j,k)
>
> If(ABS(cd-data[i-1,j-1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i, j-1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i+1,j-1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i-1,j, k]) LE mg) then f = f+1
> If(ABS(cd-data[i+1,j, k]) LE mg) then f = f+1
> If(ABS(cd-data[i-1,j+1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i, j+1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i+1,j+1,k]) LE mg) then f = f+1
>
> endif
>
> However, array subscripting with negative numbers isn't allowed by
> IDL.
>
> Has anyone any ideas how I might get around this?
>
> Many thanks!
>
> All the best
> Martin
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
|
|
|
|
Re: array subscripting problem [message #43424 is a reply to message #43423] |
Mon, 11 April 2005 07:34  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Martin writes:
> Thanks David, could you expand on that a bit?
>
> I realise it's still only breakfast time over there!
I really need to fix my coffee before I look at the newsgroup. :-(
You wanted to know how to keep array subscripts from going
negative. If you compare the subscript with 0 and choose
the larger of the two with the "greater than" operator,
you will have achieved your goal. I'm not saying it will
be *correct*, but I am saying it will *not be negative*. :-)
Similarly, you can fix problems on the other end by using
the "less than" operator and checking that the index is
smaller than the size of the array.
array = indgen(100)
s = Size(array, /Dimensions)
print, array[0 > j < (s[0]-1)]
This will keep your index between the allowed indices of
the array. Be *sure* to include those parentheses! Leaving
them off is one of the classic gotchas.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
Re: array subscripting problem [message #43425 is a reply to message #43423] |
Mon, 11 April 2005 06:38  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Martin Doyle writes:
> I've got a gridded dataset and I'm searching the grid trying to find
> those points which have a certain value. Once I've found this point, I
> want to check all points around it to see if they have the same value
> plus a little bit. This is what I've come up with:
>
> If (data(i,j,k) GE p) THEN begin
>
> cd = data(i,j,k)
>
> If(ABS(cd-data[i-1,j-1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i, j-1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i+1,j-1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i-1,j, k]) LE mg) then f = f+1
> If(ABS(cd-data[i+1,j, k]) LE mg) then f = f+1
> If(ABS(cd-data[i-1,j+1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i, j+1,k]) LE mg) then f = f+1
> If(ABS(cd-data[i+1,j+1,k]) LE mg) then f = f+1
>
> endif
>
> However, array subscripting with negative numbers isn't allowed by
> IDL.
>
> Has anyone any ideas how I might get around this?
array[0 > j]
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|