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

Home » Public Forums » archive » help with WHERE( ) function
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
help with WHERE( ) function [message #91786] Wed, 26 August 2015 09:14 Go to next message
g.nacarts is currently offline  g.nacarts
Messages: 148
Registered: November 2013
Senior Member
Hi

I have created 4 masks to count the number of elements which are non zero. I used the WHERE function to count the number but it seems that it doesn't count the nonzero elements of the 4 masks.

I typed the following

ind = where((mask1 eq 1) and (mask2 eq 1) and (mask3 eq 1) and (mask4 eq 1),count)
print, count

And I got count 0.

When I typed
ind = where(mask1 eq 1, count)
I got count 908. Which looks that it works for one mask but not for four of them.

Can anyone help with this?
Re: help with WHERE( ) function [message #91787 is a reply to message #91786] Wed, 26 August 2015 09:42 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
On 08/26/15 12:14, g.nacarts@gmail.com wrote:
> Hi
>
> I have created 4 masks to count the number of elements which are non
> zero. I used the WHERE function to count the number but it seems that
> it doesn't count the nonzero elements of the 4 masks.
>
> I typed the following
>
> ind = where((mask1 eq 1) and (mask2 eq 1) and (mask3 eq 1) and (mask4
> eq 1),count) print, count
>
> And I got count 0.
>
> When I typed ind = where(mask1 eq 1, count) I got count 908. Which
> looks that it works for one mask but not for four of them.
>
> Can anyone help with this?

Maybe you should change the "and" to "or" in your chained-together
WHERE() call?

cheers,

paulv
Re: help with WHERE( ) function [message #91788 is a reply to message #91787] Wed, 26 August 2015 09:52 Go to previous messageGo to next message
g.nacarts is currently offline  g.nacarts
Messages: 148
Registered: November 2013
Senior Member
I changed the "and" to "or" but this doesn't give me the sum of the four masks. I did it separately for the 4 masks to see how many nonzero elements are in each mask and the count number which comes out of the WHERE() using the "or" does not agree.
Re: help with WHERE( ) function [message #91789 is a reply to message #91788] Wed, 26 August 2015 10:50 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
On 08/26/15 12:52, g.nacarts@gmail.com wrote:
> I changed the "and" to "or" but this doesn't give me the sum of the
> four masks. I did it separately for the 4 masks to see how many
> nonzero elements are in each mask and the count number which comes
> out of the WHERE() using the "or" does not agree.

Um, well, without knowing anything about your data or the masks, I
wouldn't expect it to.

There may be (definitely are I would say, based on your results)
repeated indices in some of the masks.

cheers,

paulv
Re: help with WHERE( ) function [message #91790 is a reply to message #91786] Wed, 26 August 2015 16:45 Go to previous messageGo to next message
Phillip Bitzer is currently offline  Phillip Bitzer
Messages: 223
Registered: June 2006
Senior Member
On Wednesday, August 26, 2015 at 11:14:33 AM UTC-5, g.na...@gmail.com wrote:
> Hi
>
> I have created 4 masks to count the number of elements which are non zero. I used the WHERE function to count the number but it seems that it doesn't count the nonzero elements of the 4 masks.
>
> I typed the following
>
> ind = where((mask1 eq 1) and (mask2 eq 1) and (mask3 eq 1) and (mask4 eq 1),count)
> print, count
>
> And I got count 0.
>
> When I typed
> ind = where(mask1 eq 1, count)
> I got count 908. Which looks that it works for one mask but not for four of them.
>
> Can anyone help with this?

As usual, taking things step by step will help you. :-)

First, understand what mask1 EQ 1 gives you - it's an array of ones and zeros, and the array has the same dimensions as mask1 . So, you have 908 elements in mask1 that have ones.

When you do this:
(mask1 eq 1) and (mask2 eq 1)

you are "and"ing the two arrays of ones and zeros. This is done element by element. This is the important part. So, this result will also be an array of ones and zeros - elements in which both masks are equal to one will be one (1 AND 1 = 1), and zero otherwise (1 AND 0 = 0, 0 AND 1=0)

Then, you find *where* your argument is true. In this case, this will be where the "and"ed array is one. Which means this returns the indices where all the masks are equal to one.

Basically, you show that there are no elements in the four arrays in which all the masks are one.

An important debug skill is to simplify your problem to see what your code is really doing, using smallish data you can easily "see."

Take this example:
x=BYTARR(4, 4)
y=BYTARR(4, 4)

Go ahead and flip a few elements:
x[1, 1] = 1 & x[1,2] = 1
y[2, 2] = 1

You should be able to take these step by step to see what each part of your code does:
print, x EQ 1
print, X EQ 1 AND y EQ 1

Put it all together:

ind = WHERE( x EQ 1 AND y EQ 1, count)
----count is zero - no elements are both one

Flip another element:
y[1, 2] = 1
ind = WHERE( x EQ 1 AND y EQ 1, count)

Now, count is 1 since element [1, 2] is one in both arrays.

What the heck, go ahead and flip another one:
y[1, 1] = 1
ind = WHERE( x EQ 1 AND y EQ 1, count)

Now, count is two since both [1, 2] and [1, 1] is equal to one in both arrays.

If you want to simply "count the number of elements which are non zero", then just do a WHERE on each mask and sum the counts.

Finally, if your masks only contain ones and zeros, there's nothing gained by doing x EQ 1.
ind = WHERE( x AND y, count)
Re: help with WHERE( ) function [message #91794 is a reply to message #91790] Thu, 27 August 2015 05:56 Go to previous messageGo to next message
g.nacarts is currently offline  g.nacarts
Messages: 148
Registered: November 2013
Senior Member
What you said it makes sense. Thanks Phillip, I didn't realize that this was happening.

I want the indices where are not zero. So I cannot just sum up the counts.

Even using ind=WHERE(x AND y AND z AND w, count) the result is zero
Re: help with WHERE( ) function [message #91795 is a reply to message #91794] Thu, 27 August 2015 06:44 Go to previous messageGo to next message
Yngvar Larsen is currently offline  Yngvar Larsen
Messages: 134
Registered: January 2010
Senior Member
On Thursday, 27 August 2015 14:56:55 UTC+2, g.na...@gmail.com wrote:
> What you said it makes sense. Thanks Phillip, I didn't realize that this was happening.
>
> I want the indices where are not zero. So I cannot just sum up the counts.

Which indices should not be zero? You have 4 different masks.

x = mask1 ne 0
y = mask2 ne 0
z = mask3 ne 0
w = mask4 ne 0

Some possibilities:
*) All masks are 1 -> where(x AND y AND z AND w) ;apparently, you have no such pixels
*) At least one mask is 1 -> where(x OR y OR z OR w)
*) At least two masks are 1 -> where((x + y + z + w) ge 2)
*) Something else?

Which one do you want?

--
Yngvar
Re: help with WHERE( ) function [message #91800 is a reply to message #91795] Sat, 29 August 2015 07:50 Go to previous message
karo03de is currently offline  karo03de
Messages: 21
Registered: March 2007
Junior Member
Am Donnerstag, 27. August 2015 15:44:10 UTC+2 schrieb Yngvar Larsen:
> On Thursday, 27 August 2015 14:56:55 UTC+2, g.na...@gmail.com wrote:
>> What you said it makes sense. Thanks Phillip, I didn't realize that this was happening.
>>
>> I want the indices where are not zero. So I cannot just sum up the counts.
>
> Which indices should not be zero? You have 4 different masks.
>
> x = mask1 ne 0
> y = mask2 ne 0
> z = mask3 ne 0
> w = mask4 ne 0
>
> Some possibilities:
> *) All masks are 1 -> where(x AND y AND z AND w) ;apparently, you have no such pixels
> *) At least one mask is 1 -> where(x OR y OR z OR w)
> *) At least two masks are 1 -> where((x + y + z + w) ge 2)
> *) Something else?
>
> Which one do you want?
>
> --
> Yngvar

I would recommend for "Something else?"

ind=[where((mask=[[[mask1]],[[mask2]],[[mask3]],[[mask4]]]) ne 0,count)]

Hopefully each nonzero element will be found now! And the index list is useful for mask!

Karsten
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Printf
Next Topic: Icrease a elements in a array

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

Current Time: Wed Oct 08 15:17:37 PDT 2025

Total time taken to generate the page: 0.03308 seconds