How to get the minimum value in a two-dimensional array for a given index?

double[] tab = new double[10]; 

I know that I can generate a minimum of tab.Min() .

 double[,] tab = new double[10,2]; 

This is a coordinate table, in the second index 0 is x and 1 is y. There are 10 points.

How to get the minimum (and maximum) value of x and y?

In other words:

minX - the smallest value in the 1st column (second index = 0, for example tab[xxx, 0] );
minY - the smallest value in the second column (second index = 1, for example tab[xxx, 1] );

+4
source share
2 answers
 var doubles = new double[4,2]{{1,2},{4,5},{7,8},{9,1}}; var min = System.Linq.Enumerable.Range(0, 4).Select(i => doubles[i, 1]).Min(); 

OR

 var doubles = new double[4,2]{{1,2},{4,5},{7,8},{9,1}}; var min = System.Linq.Enumerable.Range(0, doubles.GetUpperBound(0)+1) .Select(i => doubles[i, 1]).Min(); 
+6
source
 double minX = tab[0,0], minY = tab[0,1]; String coordinate = "X"; foreach (double number in tab) { if (coordinate == "X") { if(number < minX) minX = number; coordinate = "Y"; } else if (coordinate == "Y") { if (number < minY) minY = number; coordinate = "X"; } } 
0
source

Source: https://habr.com/ru/post/1334846/


All Articles