This question is a continuation of a previous thread to compare two lists with the same length:
Is there any efficient easy way to compare two lists with the same length with Mathematica?
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.
Here's my code:
Do[If[NonNegative[Min[H[[i]] - h]], h = H[[i]], ## &[]], {i, h = H[[1]]; 1, Length[H]}];h
Any better trick to do this?
EDIT:
I want to define this as a function like:
maxList[H_]:=Do[If[NonNegative[Min[H[[i]] - h]], h = H[[i]], ## &[]], {i, h = H[[1]]; 1, Length[H]}];h
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_] := Module[{h = H[[1]]}, Do[If[NonNegative[Min[H[[i]] - h]], h = H[[i]], ## &[]], {i, Length[H]}]; h]
or
maxList[H_]:=Last[Table[If[NonNegative[Min[H[[i]] - h]], h = H[[i]], ## &[]], {i, h = H[[1]]; 1, Length[H]}]]
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, {}]]
We test the both methods with
test = RandomInteger[100, {500000, 10}];
test1 = Insert[test, Table[100, {10}], RandomInteger[{1, 500000}]];
and we get
In[5]:= maxList[test] // Timing
maxListFast[test] // Timing
Out[5]= {2.761, {}}
Out[6]= {0.265, {}}
and
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}}
EDIT
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
timings close up.