Re: Count using pattern on two different matrices
- To: mathgroup at smc.vnet.net
- Subject: [mg57092] Re: Count using pattern on two different matrices
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Sun, 15 May 2005 03:03:46 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 5/14/05 at 4:58 AM, leenewm at umich.edu (Lee Newman) wrote: >I have two matrices with an equal number of rows, but not >necessarily columns, for example: >L1= {{True,False},{False, True},{False, False}, etc.....} ; L2= >{{True,False,False},{True, True,True},{False, True,False}, >etc.....} ; >I would like to compute a count of the number of rows in which one >of the columns of L1 meets some criterion, and one of the columns >of L2 meets some criterion, i.e. If L1[[row,2 ]]==False and >L2[[row,3]]==True then this row contributes 1 to the count. >I suspect that there is an elegant way to do this, perhaps using >Count[] and MapThread, but I can't figure it out other than >resorting to a rather clumsy For or Do loop that would check line >by line and maintain a counter. >Any help would be appreciated, Lee One way to accomplish what you want would be In[1]:= L1 = {{True, False},{False, True}, {False, False}}; L2 = {{True, False, False}, {True, True, True}, {False, True, False}}; In[3]:= Count[Equal@@@Transpose@{L1[[All, 2]], L2[[All, 3]]}, False] Out[3]= 0 Checking we see In[4]:= L1[[All,2]] Out[4]= {False, True, False} and In[5]:= L2[[All,3]] Out[5]= {False,True,False} Clearly, both are the same verifying the count above is correct. An alternative to In[3] using MapThread to do the same task would be: In[6]:= Count[MapThread[#1 == #2 &, {L1[[All, 2]], L2[[All, 3]]}], False] Out[6]= 0 -- To reply via email subtract one hundred and four