Nonzero Min[]

• To: mathgroup at smc.vnet.net
• Subject: [mg43447] Nonzero Min[]
• From: Petr Kujan <kujanp at fel.cvut.cz>
• Date: Wed, 17 Sep 2003 07:59:05 -0400 (EDT)
• Organization: Czech Technical University
• Sender: owner-wri-mathgroup at wolfram.com

```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 *)
(*************************************************************)