Reply to topic  [ 5 posts ] 
Is there a way to see if a value matches in multiple arrays? 
Author Message
Yorick Padawan

Joined: Tue Jun 07, 2011 1:46 pm
Posts: 32
Location: UNH
Post Is there a way to see if a value matches in multiple arrays?
Hi, without reinventing the wheel is it possible to get the indexes of matching values in multiple arrays?

Code:
func common(r, p, r1, p1)
/* DOCUMENT common(r, p, r1, p1)
   where r and p are 2 separate, 1D, same sized arrays, r1 and p1 are the values to check for
   returns index
*/
{
   n = numberof(r);
   if (numberof(p) != n)
      error, "common(): Arrays have to have the same size.";
   index1 = where(r == r1);
   index2 = where(p == p1);
   index = nil;
   for (i = 1; i <= n; i++)
   {
      if (index1 == index2(i))
         index = grow(index, i);
   }
   return index;
}


Ronak


Mon Mar 12, 2012 9:35 am
Profile
Yorick Master

Joined: Mon Nov 22, 2004 9:43 am
Posts: 354
Location: Livermore, CA, USA
Post Re: Is there a way to see if a value matches in multiple arr
Your sample code is broken (in addition to being hideously inefficient), so I can't be sure what you want to do. If you simply want a list of indices where both r==r1 and p==p1, just do this:

Code:
list = where((r==r1) & (p==p1));


In general, you shouldn't need any explicit loops, and you shouldn't use the "where" function until you have constructed a bit mask that is set for the conditions you want met. The code here will automatically fail with a conformability error if r and p are not similar shapes; it will work for any dimensionality of r, r1, p, and p1, although the resulting index list will only be usable on the ones which have the same shape as the argument to "where".

If this isn't what you meant your "common" function to do, you'll need to write a more intelligible document comment... Often that's the hardest part, so I'm not being funny here!


Tue Mar 13, 2012 6:40 pm
Profile
Yorick Padawan

Joined: Tue Jun 07, 2011 1:46 pm
Posts: 32
Location: UNH
Post Re: Is there a way to see if a value matches in multiple arr
That is absolutely perfect. Thank you.

My two arrays had the exact same dimensions. I didn't realize that I could put an ampersand in the where statement.


Wed Mar 14, 2012 8:15 am
Profile
Yorick Padawan

Joined: Tue Jun 07, 2011 1:46 pm
Posts: 32
Location: UNH
Post Re: Is there a way to see if a value matches in multiple arr
My apologies for the poorly written function. I actually didn't test it because I knew there was an easier way to do it but I couldn't figure it out. I looked in the documentation of the where function as well. Perhaps this would be a good addition the where function's documentation?


Wed Mar 14, 2012 8:31 am
Profile
Yorick Guru

Joined: Thu May 10, 2007 12:07 pm
Posts: 62
Post Re: Is there a way to see if a value matches in multiple arr
Hi rpv25 !

The where function does not accept an ampersand as you wrote. The '&' is interpreted before and the result sent to the where function.
e.g. you can also write:
Code:
test =  (r==r1 ) & (p==p1)
list = where( test)


Note that if you want to test where two 1D array (with different or equal dimensions) have the same value you can use this trick :
Code:
a = [1, 2, 3, 4, 5]
b = [4, 1]

test = a(, -:1:numberof(b)) == b(-:1:numberof(a), ) /*  test will be a 5x2 matrices with 1/0 integer */
pli, test /* For visual representation */

where2(test) /*  Will give you the pair matrice-coordinates where values are matched */

/* alternatively you can use for instance : */
where ( test( , sum))  /*  will return the index of the array 'a'  where at least 1 value match 'b'   in this case  [4,1] */
where ( test( sum, ))  /*  will return the index of the array 'b'  where at least 1 value match 'a'   in this case  [1,2] */
/* etc .. */

Sure, this work well if the arrays 'a' and 'b' are not too big otherwise you can blow memory by creating a big 'test' array.

Cheers,
Sylvain.


Thu Mar 15, 2012 10:50 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.