Miko Kronn
Miko Kronn

Reputation: 2454

How can I get minimum value in two dimensional array for given index?

double[] tab = new double[10];

I know I can gen minimum by tab.Min().

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

This is table of coordinates, in 2nd index 0 is x and 1 is y. There are 10 points.

How can I get minimum (and maximum) value of x and y?

In other words:

minX is the smallest value in 1st column (second index=0 e.g tab[xxx, 0]);
minY is the smallest value in 2nd column (second index=1 e.g tab[xxx, 1]);

Upvotes: 6

Views: 10190

Answers (2)

user1234567
user1234567

Reputation: 613

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";
    }
}

Upvotes: 0

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

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();

Upvotes: 7

Related Questions