Re: Nonzero Min[]
- To: mathgroup at smc.vnet.net
- Subject: [mg43510] Re: Nonzero Min[]
- From: "Peter Pein" <peter1963 at totalise.co.uk>
- Date: Thu, 18 Sep 2003 05:40:05 -0400 (EDT)
- References: <bk9jgo$t9c$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Petr, the fastest I was able to find is: Min[DeleteCases[dat,0]]. It is 3.4 times faster than your findmin3[] (Mathematica 4.0 on Windoze). Peter "Petr Kujan" <kujanp at fel.cvut.cz> schrieb im Newsbeitrag news:bk9jgo$t9c$1 at smc.vnet.net... > Hello MathGroup, > I need function which finds nonzero minimum in the list. My > definitions are now slower in comparison with kernel function Min[ ]. > > > findmin1[data_] := Min[data] > findmin1[dat] // Timing (* 0.01 Second*) (* Very good time consumption > but wrong result for me. *) > > > It's possible shorter time consumption for this problem? > > (*************************************************************) > My definition are now: > > (* Testing data are Integer or Real numbers (possible mixed). *) > SeedRandom[1]; > dat = Abs[Join[Array[Random[ > Integer, {-10, 10}]&, {10^4}], Array[Random[Real, {-10, 10}]&, > {10^4}]]]; > > (*** 2. - Select ***) > findmin2[data_] := Min[Select[data, Not[ZeroQ[#]]&]]; > findmin3[dat] // Timing > (* 0.12 Second *) > > (*** 3. - Sort ***) > dropfirstzeros[dat_List] := Block[{i}, i = 1; > While[ZeroQ[dat[[i]]], i = i + 1;]; > Drop[dat, i - 1]]; > findmin2[data_] := dropfirstzeros[Sort[data]][[1]]; > findmin2[dat] // Timing > (* 0.14 Second *) > > (*** 4. - Map ***) > getnznum[list_] := Block[{i = 1}, > While[ZeroQ[list[[i]]], i++]; list[[i]] > ]; > findmin4[data_] := Block[{minim = getnznum[data]}, > If[(# > 0) && (minim > #), minim = #] & /@ data; > minim > ]; > findmin4[dat] // Timing > (* 0.26 Second *) > > (*** 5. - Do ***) > getnznum[list_] := Block[{i = 1}, > While[ZeroQ[list[[i]]], i++]; list[[i]] > ]; > findmin5[data_] := Block[{minim, p}, > minim = getnznum[data]; > Do[ > p = data[[i]]; > If[((p > 0) && (minim > p)), minim = p], > {i, 1, Length[data]} > ]; > minim > ] > findmin5[dat] // Timing > (* 0.32 Second *) > (*************************************************************) > > Thank in advance for all your help. > Best regards, > Petr. >