comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: pointers and IF statement
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: pointers and IF statement [message #46324] Thu, 17 November 2005 11:17
JJMeyers2 is currently offline  JJMeyers2
Messages: 12
Registered: October 2005
Junior Member
Thank you both for your help.
I will try the histogram, it seems it might be faster for what I am
doing!
Re: pointers and IF statement [message #46331 is a reply to message #46324] Wed, 16 November 2005 23:30 Go to previous message
peter.albert@gmx.de is currently offline  peter.albert@gmx.de
Messages: 108
Registered: July 2005
Senior Member
Hi,

I am not sure whether I get you right, but what you are looking for are
those indices where both lists show the same value, right? I guess,
with the line

WHERE(list1-list2(i) EQ min(list1-list2(i))

you try to find those indices where list1 is closest to list2[i]. If
so, you should use

min(abs(list1-list2[i])) instead, as otherwise the minimum value is
where the largest negative difference occurs, not the one closest to
zero.

Apart from that, I'd strongly suggest to follow the suggestion to use
square brackets [] for indexing and round brackets () for function
calls. It makes the code better readable and helps avoiding confusion
if you happen to name a variable after a function.

As for the error message, Ben's comment is perfectly right, generally
WHERE returns an vector, so you have to check its first value against
-1, which luckily does not throw an error if the rreturn value *is* -1,
as you can index scalars with [0]. On the other hand, if you do
something like

index = WHERE(this EQ that, n)

then "n" gives the number of matches, so you could continue with a line
like

IF n gt 0 THEN do_the_rest

And now for your original problem. If you are dealing with integer
values, I'd suggest using histogram instead of the for loop. Something
like this:

Just a few example numbers:

list1 = [0,0,1,1,2,2,3,4,5,5,5]
list2 = [0,1,2,3,4]

We'll use histogram, so make sure to use the same min and max values in
both calls:

min_value = min(list1) < min(list2)
max_value = max(list1) > max(list2)
h1 = histogram(list1, min = min_value, max = max_value, reverse = r1)
h2 = histogram(list2, min = min_value, max = max_value, reverse = r2)

Now, the key is the reverse index, which is just plain confusing when
you first look at it, but it just basically tells you the indices where
the list values do fall in each histogram bin.

If you want to see where list1 and list2 equal, say, 1, you can use

print, list1[r1[r1[1]:r1[2]-1]]
print, list2[r2[r2[1]:r2[2]-1]]

(just printing a lot of "1"s)

which means that
r1[r1[1]:r1[2]-1] gives you the indices where list1 equals 1, while
r2[r2[1]:r2[2]-1] gives the appropriate indices for list2.

Well, you are paying with weird looking code, but you'll gain a lot of
processing speed.

If this approach seems to work for you, then take the time to read JD
Smith's histogram tutorial at
http://www.dfanning.com/tips/histogram_tutorial.html

Cheers,

Peter
end
Re: pointers and IF statement [message #46337 is a reply to message #46331] Wed, 16 November 2005 12:27 Go to previous message
btt is currently offline  btt
Messages: 345
Registered: December 2000
Senior Member
JJMeyers2@gmail.com wrote:
> Hello,
>
> I am trying to learn pointers and I had a problem while writing a
> program.
> My program is comparing two lists of data (list1,list2) and is trying
> to find in which positions the data match, but because that might be in
> more than 2 positions I am using the pointers to store the data. In the
> case that the program does not find a match I want to be able to
> substitute with another value.
> So my program goes like this:
>
> match1=ptarr(number,/allocate_heap)
> FOR i=0,number-1 DO BEGIN
> *match1[i]=WHERE(list1 GT list2(i))
> IF *match1[i] EQ -1 THEN BEGIN
> *match1[i]=WHERE(list1-list2(i) EQ min(list1-list2(i))
> ENDIF
> ENFOR
>
> When i try to run this i get:
> Expression must be a scalar or 1 element array in this context: <BYTE
> Array[2]>.
> and I can not figure out how to correct the problem.
>

I would suspect that *match[i] is not a scalar as you are assuming.
That is why you get the error asking for a scalar.

Perhaps you want

IF (*match1[i])[0] EQ -1 THEN BEGIN

note the indexing of the vector stored in *match[i].

Sante,
Ben
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDLgrModel rotation axis's origin
Next Topic: Re: IDLgrModel rotation axis's origin

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:48:12 PDT 2025

Total time taken to generate the page: 0.00405 seconds