day0ops
day0ops

Reputation: 7482

C# how get maximum value from list of lists

I have a list of lists that I have created from a "tab" delimited string. Instead of a 2d array i use list of lists as I dont know the size to create the array.

It will be something like,

         0         0
   16.0000         0
   15.0000   15.0000
         0   15.0000
    2.7217    5.6904
    3.7217    5.6904

I now want to find the maximum and minimum from each of the columns. So if you take the above example,

maximum would be : 16.0000 15.0000 and minimum would be : 0 0

for (int i = 0; i < size[0]; i++)
{
  // Create size[0] x size[1] array
  obstVertex.Add(new List<double>());
  for (int y = 0; y < size[1]; y++)
  {
    obstVertex[i].Add(Convert.ToDouble(split[i+(y*size[0])]));
  }
}

How can I find the maximum or the minimum value using linq ???

Thanks in advance

Upvotes: 0

Views: 11367

Answers (4)

shenhengbin
shenhengbin

Reputation: 4294

    static void Main(string[] args)
    {

        List<List<double>> lists = new List<List<double>>() { 
            new List<double>(){0 , 0 } ,
            new List<double>(){16.0000 , 0 } ,
            new List<double>(){16.0000 , 15.0000 } ,
            new List<double>(){0 , 15.0000 } ,
            new List<double>(){2.7217 , 5.6904 } , 
            new List<double>(){3.7217 , 5.6904 } 
        };

        var r =    new {
                         col1_max = (from x in lists select x[0]).Max(),
                         col1_min = (from x in lists select x[0]).Min(),
                         col2_max = (from x in lists select x[1]).Max(),
                         col2_min = (from x in lists select x[1]).Min(), 
                     }; 


        Console.WriteLine(string.Format("col1_max = {0}\r\ncol1_min = {1}\r\ncol2_max = {2}\r\ncol3_max = {3}", r.col1_max , r.col1_min , r.col2_max , r.col2_min));


        Console.Read();
    }

Upvotes: 1

sloth
sloth

Reputation: 101052

Try the following:

class Program
{
    static void Main(string[] args)
    {
        var l = new List<List<double>>() {new List<Double>() {0, 16.0000, 15.0000, 0, 2.7217, 3.7217}, 
                                          new List<Double>() {0, 0, 15.0000, 15.0000, 5.6904, 5.6904}};

        int i = 1;
        var result = from sublist in l
                     select new { min = sublist.Min(), max = sublist.Max(), index = i++ };

        foreach (var r in result)
            Console.WriteLine(String.Format("Index: {0} Min: {1} Max: {2}",r.index,  r.min, r.max));

        Console.ReadKey();
    }
}

It will get the min- and max-value for each sub-list in the list of lists.

Upvotes: 4

Tim Rogers
Tim Rogers

Reputation: 21713

I assume obstVertex is declared like this:

List<List<double>> obstVertex;

Then you can do this

var minima = Enumerable.Range(0, obstVertex[0].Count - 1)
    .Select (colIndex => obstVertex.Select(row => row[colIndex]).Min())

to get all the minimum values of each column.

Upvotes: 1

Petar Ivanov
Petar Ivanov

Reputation: 93030

List<List<double>> myList;

myList.Add(new List(new double[] { 0, 16, 15, 0, 2.7217, 3.7217 }));
myList.Add(new List(new double[] { 0, 0, 15, 15, 5.6904, 5.6904 }));

List<double> maxList = myList.Select(l => l.Max()).ToList();
List<double> minList = myList.Select(l => l.Min()).ToList();

Upvotes: 5

Related Questions