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

Home » Public Forums » archive » Re: extracting values from an array
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: extracting values from an array [message #44252] Thu, 02 June 2005 17:32
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article <1117744798.399372.98360@g14g2000cwa.googlegroups.com>,
caitouer@yahoo.com wrote:

> I normally do this:
> b=a(where(a gt -1))
> then b=[2,3,4]
> Cheers,
> Caitouer
>

That's OK until WHERE returns a -1 (nothing found). For robust code,
check the COUNT values.

Ken Bowman
Re: extracting values from an array [message #44254 is a reply to message #44252] Thu, 02 June 2005 14:01 Go to previous message
caitouer is currently offline  caitouer
Messages: 21
Registered: June 2005
Junior Member
I normally do this:
b=a(where(a gt -1))
then b=[2,3,4]
Cheers,
Caitouer
Re: extracting values from an array [message #44276 is a reply to message #44254] Wed, 01 June 2005 13:14 Go to previous message
Michael Wallace is currently offline  Michael Wallace
Messages: 409
Registered: December 2003
Senior Member
>> a = [2,-1, -1, 3,-1,-1, 4, -1]
>> w = where(a gt -1, count)
>> if count gt -1 then b = a[w]
>>
>
> I think you want
>
> IF count GT 0 THEN ...

Oops. You're right. With all the -1's floating about in the example, I
just wanted to put -1 everywhere. :-)

-Mike
Re: extracting values from an array [message #44277 is a reply to message #44276] Wed, 01 June 2005 13:06 Go to previous message
Benjamin Hornberger is currently offline  Benjamin Hornberger
Messages: 258
Registered: March 2004
Senior Member
Michael Wallace wrote:
>
> If you want something a little cleaner, I'd suggest using the count
> parameter set by the where command rather than checking if w is -1 or
> not. I just find this a little easier to read.
>
> a = [2,-1, -1, 3,-1,-1, 4, -1]
> w = where(a gt -1, count)
> if count gt -1 then b = a[w]
>

I think you want

IF count GT 0 THEN ...

Cheers,
Benjamin
Re: extracting values from an array [message #44278 is a reply to message #44277] Wed, 01 June 2005 12:49 Go to previous message
Michael Wallace is currently offline  Michael Wallace
Messages: 409
Registered: December 2003
Senior Member
> Actually I thought maybe there is a shortcut like:
> B = A gt -1
>
> But if
> A = [2,-1, -1, 3,-1,-1, 4, -1]
> the command
> B = A gt -1
> leads to
> B = [1,0,0,1,0,0,1,0]
> which this doesn't help me as I want B = [2,3,4]


In this case, the "a gt -1" returns a boolean value based on whether the
expression is true or not. That's why you see the array of only 1
(true) and 0 (false). Also note that whenever you do array operations,
your resultant array will be the same size as the input array. You want
your output array to be potentially smaller and therefore you need at
least one call to a function such as "where" to handle the manipulation.
By the way, the problem you describe is the very reason why where was
invented.

If you want something a little cleaner, I'd suggest using the count
parameter set by the where command rather than checking if w is -1 or
not. I just find this a little easier to read.

a = [2,-1, -1, 3,-1,-1, 4, -1]
w = where(a gt -1, count)
if count gt -1 then b = a[w]


> The array A is never bigger than 50 elements.
> So I will keep my three lines of code.

All of us like to tinker and find some slick way of doing things, but if
the code works as it should and there's no real gain to be achieved from
optimizing, don't optimize! And never optimize just for the sake of
optimization. Profile your code, find the major bottlenecks and
optimize those first. Rinse, wash, repeat.

Of course, if you're just tinkering with something, playing around with
it to see how it works, optimize the crap out of it. It's a good
learning experience. I still have fond memories of taking one of my
co-worker's programs and bringing its running time from about 20 minutes
down to under a minute and even under 30 seconds in some cases. And I
could have taken it down further had I written some DLMs. :-)

-Mike
Re: extracting values from an array [message #44279 is a reply to message #44278] Wed, 01 June 2005 12:19 Go to previous message
Francois L. is currently offline  Francois L.
Messages: 19
Registered: December 2004
Junior Member
Hello,

Actually I thought maybe there is a shortcut like:
B = A gt -1

But if
A = [2,-1, -1, 3,-1,-1, 4, -1]
the command
B = A gt -1
leads to
B = [1,0,0,1,0,0,1,0]
which this doesn't help me as I want B = [2,3,4]

The array A is never bigger than 50 elements.
So I will keep my three lines of code.

Thank you,

Fran�ois.

"R.G. Stockwell" <no@email.please> wrote in message
news:6znne.23$3A1.1578@news.uswest.net...
> "Francois L." <fleduc@lycos.com> wrote in message
> news:1117651509.818205@news.drenet.dnd.ca...
>> Hello,
>>
>> Probably a simple question...
>>
>> I have an array A and I want to extract values greater than -1.
>>> A = [2,-1, -1, 3,-1,-1, 4, -1]
>>> w = where(a gt -1)
>>> if w[0] gt -1 then b = a[w]
>>
>> Is there a cleaner and faster way to do this ?
>>
>> Thanks,
>>
>> Fran�ois.
>
>
> Hi,
>
> cleaner? I doubt it, I think where() is pretty clean :) and I don't think
> writing
> two lines of code is being excessively verbose.
>
> faster? you could try a histogram call, with the reverse indices keyword,
> since histogram is a magic routine that, when used non-intuitively,
> usually leads to superior results. Use 2 bins, -1000 to -1, and 0 to 1000
> for
> instance, where 1000 is chosen to be larger than any of your data.
> This may be less clean than the where() call.
>
> If, however, you are looping through a large number of A vectors,
> perhaps you could do a histogram call on all of them (or something like
> that).
> Or if you are looping over different criteria (gt -1, then gt 1, etc) that
> could
> be accomplished in one histogram call.
>
> Exactly how big is this A array that you are not satisfied with the speed
> of where?
>
> Cheers,
> bob
>
Re: extracting values from an array [message #44280 is a reply to message #44279] Wed, 01 June 2005 11:58 Go to previous message
R.G. Stockwell is currently offline  R.G. Stockwell
Messages: 363
Registered: July 1999
Senior Member
"Francois L." <fleduc@lycos.com> wrote in message
news:1117651509.818205@news.drenet.dnd.ca...
> Hello,
>
> Probably a simple question...
>
> I have an array A and I want to extract values greater than -1.
>> A = [2,-1, -1, 3,-1,-1, 4, -1]
>> w = where(a gt -1)
>> if w[0] gt -1 then b = a[w]
>
> Is there a cleaner and faster way to do this ?
>
> Thanks,
>
> Fran�ois.


Hi,

cleaner? I doubt it, I think where() is pretty clean :) and I don't think
writing
two lines of code is being excessively verbose.

faster? you could try a histogram call, with the reverse indices keyword,
since histogram is a magic routine that, when used non-intuitively,
usually leads to superior results. Use 2 bins, -1000 to -1, and 0 to 1000
for
instance, where 1000 is chosen to be larger than any of your data.
This may be less clean than the where() call.

If, however, you are looping through a large number of A vectors,
perhaps you could do a histogram call on all of them (or something like
that).
Or if you are looping over different criteria (gt -1, then gt 1, etc) that
could
be accomplished in one histogram call.

Exactly how big is this A array that you are not satisfied with the speed of
where?

Cheers,
bob
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Python reader for IDL save files.
Next Topic: GUI states

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

Current Time: Wed Oct 08 19:13:16 PDT 2025

Total time taken to generate the page: 0.00740 seconds