Re: Nonzero Min[]
- To: mathgroup at smc.vnet.net
- Subject: [mg43479] Re: Nonzero Min[]
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 18 Sep 2003 05:38:41 -0400 (EDT)
- Organization: Universitaet Leipzig
- References: <bk9jgo$t9c$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi, and ZeroQ[0] = True ZeroQ[0.] = True ZeroQ[_] := False findmin1[data_] := Min[data /. _?ZeroQ :> Sequence[]] is not faster ?? Regards Jens Petr Kujan wrote: > > 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.