有没有什么有效的简单的方法来两个清单数学与相同长度的比较? [英] Is there any efficient easy way to compare two lists with the same length with Mathematica?
问题描述
由于两个列表 A = {A1,A2,A3,...一个}
和 B = {B1,B2,B3,。 ..bn}
,我想说 A> = B
当且仅当所有的嗳> = BI
。
Given two lists A={a1,a2,a3,...an}
and B={b1,b2,b3,...bn}
, I would say A>=B
if and only if all ai>=bi
.
有两个列表的内置逻辑比较, A ==乙
,但没有 A> b
。
我们需要这样的每个元素比较
There is a built-in logical comparison of two lists, A==B
, but no A>B
.
Do we need to compare each element like this
和@@表[A [[I]]> = B [我],{I,N}]
任何更好的技巧来做到这一点?
Any better tricks to do this?
编辑:
大感谢大家。
Great thanks for all of you.
下面是一个进一步的问题:
Here's a further question:
如何找到最大列表(如果存在)N名单中?
How to find the Maximum list (if exist) among N lists?
<一个href=\"http://stackoverflow.com/questions/8889583/any-efficient-easy-way-to-find-the-maximum-list-among-n-lists-with-the-same-leng\">Any高效简便的方法来找到使用Mathematica相同长度N名单中最大的名单?
推荐答案
方法1 :我preFER此方法。
Method 1: I prefer this method.
NonNegative[Min[a - b]]
方法2 :这是只是为了好玩。正如列昂尼德指出,它被赋予有点不公平的优势为我所用的数据。
如果一个人做两两比较,并返回False,并且打破在适当的时候,
再一个循环可能更有效(虽然我一般在顺MMA循环):
Method 2: This is just for fun. As Leonid noted, it is given a bit of an unfair advantage for the data I used. If one makes pairwise comparisons, and return False and Break when appropriate, then a loop may be more efficient (although I generally shun loops in mma):
result = True;
n = 1; While[n < 1001, If[a[[n]] < b[[n]], result = False; Break[]]; n++]; result
10 ^ 6个号码中列出了一些时间比较:
a = Table[RandomInteger[100], {10^6}];
b = Table[RandomInteger[100], {10^6}];
(* OP's method *)
And @@ Table[a[[i]] >= b[[i]], {i, 10^6}] // Timing
(* acl's uncompiled method *)
And @@ Thread[a >= b] // Timing
(* Leonid's method *)
lessEqual[a, b] // Timing
(* David's method #1 *)
NonNegative[Min[a - b]] // Timing
编辑:我删除了时序为我的方法2,因为它们可能会产生误导。和方法1更适合作为一个通用方法。
I removed the timings for my Method #2, as they can be misleading. And Method #1 is more suitable as a general approach.
这篇关于有没有什么有效的简单的方法来两个清单数学与相同长度的比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!