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 
Return to the default flat view Create a new topic Submit Reply
Re: help with WHERE( ) function [message #91790 is a reply to message #91786] Wed, 26 August 2015 16:45 Go to previous messageGo to previous 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)
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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 17:39:23 PDT 2025

Total time taken to generate the page: 0.00434 seconds