1
$\begingroup$

Possible Duplicate:
Applying And to lists of Booleans

I have two lists filled with logicals:

demondata = {True, True, False, True}
numerdata = {True, False, True, True}

I would like to find which corresponding cells positions are both True. The only code I could come up with is:

toInclude = MapThread[And, {demondata, numerdata}];

Is there a way to do something like:

demondata && numerdata

Not that there is something wrong with MapThread, it just seems more complicated than I thought it would be. In MATLAB, I can do:

list1 & list2  _or_  list1 > 10   etc ...
$\endgroup$

marked as duplicate by Artes, rm -rf Oct 25 '12 at 22:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    $\begingroup$ By the way... The word that goes with numerator is denominator. On the other hand, Demonimator sounds like an excellently cheesy sci-fi horror flick. :) $\endgroup$ – Rahul Oct 25 '12 at 22:39
  • $\begingroup$ @artes I've a feeling that my answer may address this question , but is not a valid answer to the proposed duplicate ? $\endgroup$ – image_doctor Oct 25 '12 at 23:55
5
$\begingroup$

You need use MapThread as

MapThread[And,{demondata,numerdata}]
(* {True, False, False, True} *)

Perhaps

 demondata && numerdata // Thread 

is close enough?

And ... there is also:

Inner[And, demondata, numerdata, List]
$\endgroup$
4
$\begingroup$

MapThread isn't that complicated but if you are looking for alternatives, two options spring to mind:

And @@@ Transpose[{demondata, numerdata}]
(* {True, False, False, True} *)

Or if you don't mind switching to 0s and 1s instead of logical values:

Boole[demondata]*Boole[numerdata]
(* {1, 0, 0, 1} *)
$\endgroup$
  • $\begingroup$ I just wanted to make sure I wasn't missing something more fundamental. I thought the same way you can add lists without map, you may be able to do logical operations on lists. $\endgroup$ – Stephen Lien Oct 25 '12 at 22:53
2
$\begingroup$

This gives you the positions where both are True:

{demondata, numerdata}\[Transpose]~Position~{True, True}

{{1}, {4}}

$\endgroup$
  • $\begingroup$ @rojo oops, yes you are right :) Fixed now, thank you. $\endgroup$ – image_doctor Oct 25 '12 at 23:27
  • $\begingroup$ Deleting the comment because you fixed it, +1 $\endgroup$ – Rojo Oct 25 '12 at 23:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.