admin3
(Balazs Kisfali)
January 26, 2016, 8:45am
1
Hi,
I struggle a little bit to find the best function to compare two equal length lists.
(should be compared the elements each by each, i.e a>d, b>e etc.)
list1={a,b,c} and list2{d,e,f,}
list1 > list2 ?
then I’d like to get a list of boolean like {TRUE,TRUE,FALSE} OR If{condition,“PASS”,“FAIL”}
Could you suggest the easiest way/function to do that?
Thank you
You can use the command Thread, try:
l1 = {a, b, c, d}; l2 = {e, f, g, h};
Thread[l1 > l2]
(* Out: {a>e,b>f,c>g,d>h} *)
Regards – Henrik
sghuisman
(Sander Huisman)
January 26, 2016, 11:55am
3
Try:
Thread[list1>list2]
list1 or list2 can also be a single number…
admin3
(Balazs Kisfali)
January 26, 2016, 1:18pm
4
Thank you, exactly this I was looking for …I tried “Map”, “Scan+Print”, “Condition” etc.
sghuisman
(Sander Huisman)
January 26, 2016, 5:23pm
6
Note that you can do it in more than 1 way:
list1={1,2,3}
list2={2.5,2,1.5}
Thread[list1<list2]
Map[#[[1]]<#[[2]]&,{list1,list2}\[Transpose]]
Apply[#1<#2&,{list1,list2}\[Transpose],{1}]
Apply[Less,{list1,list2}\[Transpose],{1}]
Less@@@({list1,list2}\[Transpose])
MapThread[Less,{list1,list2}]
Table[list1[[i]]<list2[[i]],{i,Length[list1]}]
I came up with these ways! and there are probably more!
murray
(Murray Eisenberg)
January 26, 2016, 6:22pm
7
And if you want to know whether every element of list1 is greater than the corresponding element of list2, then include one more thing:
And @@ Thread[list1 > list2]
sghuisman
(Sander Huisman)
January 26, 2016, 6:26pm
8
Though, generally faster would be:
AllTrue[{list1, list2}\[Transpose], Less @@ # &]
Because that one will stop after the first False is found…
admin3
(Balazs Kisfali)
January 27, 2016, 8:45am
9
Dear Gents,
It is so nice to see how this community works
Thanks for all the ideas, …of course I look for the most simplest one, and the below works well for me at the moment.
In[6]:= deal = Thread[list1 > list2]
Out[6]= {False, False, True}
However, I needed something after then the conditions was evaluated, and I was able to program only with one out put, like this
In[7]:= deal = Thread[list1 > list2] /. x_ /; x == True -> "PASS"
Out[7]= {False, False, "PASS"}
Could you please suggest maybe a way where I can pass 2 possible outputs from the boolean? Since I have false OR true output from “Thread[]” (but with condition I could pass only one output)
for instance: If x return false, then “FAIL” should be passed into the list
Thanks again
How about:
l1 = RandomInteger[2, 10];
l2 = RandomInteger[1, 10];
Thread[l1 > l2] /. {False -> "FAIL", True -> "PASS"}
(* Out (e.g.): {"FAIL","FAIL","FAIL","PASS","PASS","PASS","FAIL","PASS","PASS", "PASS"} *)
admin3
(Balazs Kisfali)
February 17, 2016, 11:30am
11
@Henrik , sorry for the late reply but I missed somehow the notification from the Wolfr community.
That was my wish to apply more output based on the boolean. Thanks a lot.
Best regards, Balazs
mthiel
(Marco Thiel)
February 17, 2016, 4:30pm
12
Hi everyone,
just for completeness if one is interested in speed:
l1 = RandomReal[1, 100000] - 0.5; l2 = RandomReal[1, 100000] - 0.5;
AbsoluteTiming[Positive[(l1 - l2)]]
where
AbsoluteTiming[Thread[l1 > l2]]
Speed might not be an issue though…
Cheers,
Marco
PS: For all the different approaches that Sander lists the respective values are:
sghuisman
(Sander Huisman)
February 17, 2016, 4:33pm
13
I’m surprised by three things:
method 3 and 4 has a big difference for the same thing.
1 is slower than 6!
and that mapthread is the fastest…