Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
"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
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 >
> 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
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
>> 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
I normally do this: b=a(where(a gt -1)) then b=[2,3,4] Cheers, Caitouer
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