[Mono-list] Minimum element of a list

David Henderson dnadavewa at yahoo.com
Fri Jan 7 17:50:56 EST 2011


Thanks, Jon and Matt!  I'll give those examples a try.

For reference, the C++ code I sent was a from memory excerpt of some code I 
wrote years ago to monotonically smooth a p-value distribution.  So, the vector 
is first sorted with ascending raw p-values and then a multiple hypothesis 
correction is computed which leaves the p-values not monotonically ascending.  
So, you then go back through the vector and make sure that the p-values only get 
bigger.  Hence, the minimum from where you are to the end as you move along what 
used to be an ordering of smallest to largest.

Dave H




----- Original Message ----
From: Jonathan Pryor <jonpryor at vt.edu>
To: David Henderson <dnadavewa at yahoo.com>
Cc: mono-list at lists.ximian.com
Sent: Fri, January 7, 2011 2:01:08 PM
Subject: Re: [Mono-list] Minimum element of a list

On Jan 7, 2011, at 4:40 PM, David Henderson wrote:
> I have a list and I would like to find the minimum element in the array from 
>the 
>
> current iterator in a foreach loop to the end, not the minimum element 
overall.

This isn't strictly possible, as there's no way of knowing the index of the 
current iterator element.  You could use List<T>.IndexOf() to find it, but this 
would return the first matching element, which doesn't help if there are 
multiple matching elements.

Consequently, I would suggest using a normal `for` loop instead of a `foreach` 
loop, at which point you will have access to the index, allowing use of 
Enumerable.Skip() and Enumerable.Min() extension methods:

    List<double> d = ...;
    for (int i = 0; i < d.Count; ++i) {
        double min_e = d.Skip (i).Min ();
    }

If you absolutely know you won't have duplicates, you could use a `foreach` loop 
and use List<T>.IndexOf(), but the performance of that would not be great...

- Jon


      


More information about the Mono-list mailing list