任何有效的简单的方法来找到其中N的最大值清单,使用Mathematica的长度相同列出? [英] Any efficient easy way to find the maximum list among N lists with the same length using Mathematica?
问题描述
这问题是一个一个previous线程的延续,两个列表具有相同长度的比较:
This question is a continuation of a previous thread to compare two lists with the same length:
<一个href=\"http://stackoverflow.com/questions/8885505/is-there-any-efficient-easy-way-to-compare-two-lists-with-the-same-length-with-m\">Is有没有什么有效的简单的方法来两个列表与数学一样长?A>
由于两个列表 A = {A1,A2,A3,...一个}
和 B = {B1,B2,B3,。 ..bn}
,我想说 A&GT; = B
当且仅当所有的嗳&GT; = BI
。现在我们有 K
列出 H = {{A11,A12,A13,... A1N},{A21,A22,A23,... A2N},{AK1,AK2,AK3,... AKN}}
,并希望找到最大的一个,如果存在。
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
. Now we have k
lists H={{a11,a12,a13,...a1n}, {a21,a22,a23,...a2n},...,{ak1,ak2,ak3,...akn}}
, and want to find the maximum one if exist.
下面是我的code:
不要[如果[非负[最小[H [我]] - H],H = H [我],##&安培; [] {I,H = H [[1]]; 1,长度[H]}]; H
任何更好的把戏做到这一点?
Any better trick to do this?
编辑:
我想将其定义为一个功能,如:
I want to define this as a function like:
maxList [H _]:=无关[如果[非负[最小[H [我]] - H],H = H [我],##&安培; [] {I,H = H [[1]]; 1,长度[H]}]; H
但问题是code上述交两行,这方面的任何解决?下面是一些code工作但不是漂亮
But the question is the code above cross two lines, any fix for this? Here is some code working but not that beautiful
maxList [H_]:=模块[{H = H [[1]]},难道[如果[非负[最小[H [我]] - H],H = H [我],##&安培; [] {我,长度[H]}]; H]
或
maxList [H _]:=最后表[如果[非负[最小[H [我]] - H],H = H [我],##&安培; ],{I,H = H [[1]]; 1,长度[H]}]]
推荐答案
Mr.Wizard的方法的修改工作的几十倍。
A modification of Mr.Wizard's approach works a few times faster.
maxListFast[list_List] := Module[{l},
l = Max /@ Transpose@list;
If[MemberQ[list, l], l, {}]]
我们测试用
test = RandomInteger[100, {500000, 10}];
test1 = Insert[test, Table[100, {10}], RandomInteger[{1, 500000}]];
和我们得到
In[5]:= maxList[test] // Timing
maxListFast[test] // Timing
Out[5]= {2.761, {}}
Out[6]= {0.265, {}}
和
In[7]:= maxList[test1] // Timing
maxListFast[test1] // Timing
Out[7]= {1.217, {{100, 100, 100, 100, 100, 100, 100, 100, 100, 100}}}
Out[8]= {0.14, {100, 100, 100, 100, 100, 100, 100, 100, 100, 100}}
修改
在一般情况下,选择一种方法,我们首先应该知道我们是对付什么样的数据。 (名单lenght,它们的数量,类型的数字)。虽然我们有大量的短名单
整数 maxListFast
作品甚至10倍以上 maxList
更好(以长度为10 500000名单的情况下)。
然而,对于实数的列表它更快只有3-4倍,而越来越长列表我们已越减慢,例如
In general, to choose a method, we should know first what kind of data we are to deal with. (lenght of lists, their number, types of numbers ). While we have a large number of short lists
of integers maxListFast
works even 10 times better (in case of 500000 lists of length 10) than maxList
.
However for lists of real numbers it is only 3-4 times faster, and the more and the longer list we have the more it slows down, e.g. :
A = RandomReal[1000, {3000, 3000}];
First@AbsoluteTiming[maxListFast@A;]/ First@AbsoluteTiming[maxList@A;]
Out[19]= 2.040516
但是如果我们插入的最大元素:
however if we insert "the greatest element" :
In[21]:= IA = Insert[A, Table[1000, {3000}], RandomInteger[{1, 3000}]];
In[22]:= First@AbsoluteTiming[maxListFast@IA;]/ First@AbsoluteTiming[maxList@IA;]
Out[22]= 0.9781931
定时收了起来。
这篇关于任何有效的简单的方法来找到其中N的最大值清单,使用Mathematica的长度相同列出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!