Message Boards Message Boards

6 Replies
7 Total Likes
View groups...
Share this post:

List-threading operations with logicals

Posted 10 years ago

{2,3,4}+3 delivers {5,6,7}

I'd like {2,3,4}==3 to deliver {False, True, False}, but it doesn't. Can I make it do that?

Jim Simons

POSTED BY: Jim Simons
6 Replies

It probably depends on what one is looking for. Nasser's suggestion seems to be the fastest one:

   (Outer[Equal, {3}, RandomInteger[3, 10^6]] //Flatten); // AbsoluteTiming
    (*{0.167448, Null}*)

It only takes a third of the time that mine takes. This is the shortest one in terms of symbols used:

    (# == 3) & /@ RandomInteger[3, 10^6]; // AbsoluteTiming
    (*{0.467847, Null}*)

This one's quite clear to read...

Thread[RandomInteger[3, 10^6] == 3]; // AbsoluteTiming
(*{0.247238, Null}*)


POSTED BY: Marco Thiel
Posted 10 years ago

Ah, I see Frank got there 5 seconds before me!!

POSTED BY: Jim Simons
Posted 10 years ago

Thanks guys, but I've discovered a different, and IMHO better, answer:



POSTED BY: Jim Simons

In[2]:= Thread[{2, 3, 4} == 3]

Out[2]= {False, True, False}

POSTED BY: Frank Kampas

Another possible way

lis = {2, 3, 4};
Outer[Equal, {3},lis] // Flatten
(* {False, True, False} *)
POSTED BY: Nasser M. Abbasi

I think that this might work:

(# == 3) & /@ {2, 3, 4}

It does appear to give the result you want.


POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract