help with WHERE( ) function [message #91786] |
Wed, 26 August 2015 09:14  |
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 #91790 is a reply to message #91786] |
Wed, 26 August 2015 16:45   |
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 #91800 is a reply to message #91795] |
Sat, 29 August 2015 07:50  |
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
|
|
|