简体   繁体   中英

Minimum value of maximum values of two dimensional Array

okay i have another question because as i told you i am trying to put all these values in result 3 as i mentioned before like this ( x , y ) then i have to get the max value of each point of this ( x , y ) for the whole points then get the min value of the maximum values , i managed to get the maximum values but when i try to get the minimum of them it turns shows zero.

This is the code.

for (int i = 0; i < result1.GetLength(0); i++)
            {
                for (int j = 0; j < result1.GetLength(1); j++)
                {
                    for (int k = 0; k < result2.GetLength(0); k++)
                    {
                        for (int m = 0; m < result2.GetLength(1); m++)
                        {


                            result3[i, j] = result1[i, j] + "," + result2[k, m];
                            Console.WriteLine(result3[i, j]);
                            if (result1[i, j] > result2[k, m])
                            {
                                highestMoment[i, j] = result1[i, j];

                            }
                            else
                            {
                                highestMoment[i, j] = result2[k, m];
                            }
                            Console.WriteLine(highestMoment[i, j]);

                            if (lowestMoment[i, j] > highestMoment[i, j])
                            {
                                lowestMoment[i, j] = highestMoment[i, j];
                            }
                            Console.WriteLine(lowestMoment[i, j]);

                            counter++;



                        }
                    }

                }

            }

and this the whole code

double[,] Cranelocations = { { -12.3256, 0.5344 }, { -12.3256, -0.4656 }, { -12.3256, -1.4656 }, { -12.3256, -2.4656 } };
 double[,] Picklocation = { { -0.3256, -3.4656 }, { 0.6744, -3.4656 }, { 1.6744, -3.4656 }, { 2.6744, -3.4656 }, { 3.6744, -3.4656 }, { 4.6744, -3.4656 }, { 5.6744, -3.4656 } };
double[,] Setlocation = { { 20.62, 5.03 }, { 24.28, 5.03 }, { 28.40, 5.03 }, { 32.11, 5.03 }, { 35.99, 5.26 }, { 40.18, 5.26 } };
double[] Weights = { 11.7865, 14.7335, 15.1015, 10.7465 };
double[,] result1 = new double[Weights.Length * Cranelocations.GetLength(0), Picklocation.GetLength(0)];
    double[,] result2 = new double[Weights.Length * Cranelocations.GetLength(0), Setlocation.GetLength(0)];
    object[,] result3 = new object[result1.GetLength(0), result1.GetLength(1)];
    double[,] highestMoment = new double[result3.GetLength(0), result3.GetLength(1)];
    double[,] lowestMoment = new double[highestMoment.GetLength(0), highestMoment.GetLength(1)];
    int counter = 0;




                    for (int m = 0; m < Weights.Length; m++)
                    {
                        int offset = m * Cranelocations.GetLength(0);

                        for (int i = 0; i < Cranelocations.GetLength(0); i++)
                        {
                            for (int j = 0; j < Picklocation.GetLength(0); j++)
                            {
                                double x = Cranelocations[i, 0] - Picklocation[j, 0];
                                double y = Cranelocations[i, 1] - Picklocation[j, 1];

                                result1[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
                            }
                        }
                    }
                    //Console.WriteLine("-----------------------------------------------------------------");

                    for (int m = 0; m < Weights.Length; m++)

                    {
                        int offset = m * Cranelocations.GetLength(0);

                        for (int i = 0; i < Cranelocations.GetLength(0); i++)
                        {
                            for (int j = 0; j < Setlocation.GetLength(0); j++)
                            {


                                double x = Cranelocations[i, 0] - Setlocation[j, 0];
                                double y = Cranelocations[i, 1] - Setlocation[j, 1];

                                result2[i +offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
                                //Console.WriteLine(result2[i, j]);

                            }

                        }
                    }




                    for (int i = 0; i < result1.GetLength(0); i++)
                    {
                        for (int j = 0; j < result1.GetLength(1); j++)
                        {
                            for (int k = 0; k < result2.GetLength(0); k++)
                            {
                                for (int m = 0; m < result2.GetLength(1); m++)
                                {


                                    result3[i, j] = result1[i, j] + "," + result2[k, m];
                                    Console.WriteLine(result3[i, j]);
                                    if (result1[i, j] > result2[k, m])
                                    {
                                        highestMoment[i, j] = result1[i, j];

                                    }
                                    else
                                    {
                                        highestMoment[i, j] = result2[k, m];
                                    }
                                    Console.WriteLine(highestMoment[i, j]);

                                    if (lowestMoment[i, j] > highestMoment[i, j])
                                    {
                                        lowestMoment[i, j] = highestMoment[i, j];
                                    }
                                    Console.WriteLine(lowestMoment[i, j]);

                                    counter++;







                                }
                            }

                        }

                    }

You are never assigning lowestMoment before comparing it to highestMoment . Since the default value of a double is 0 then lowestMoment will always be lower than anything you compare it to giving your results of 0.

This might be what you're looking for:

for (int i = 0; i < result1.GetLength(0); i++)
{
    int iOffset = i * result1.GetLength(1);

    for (int j = 0; j < result1.GetLength(1); j++)
    {
        for (int k = 0; k < result2.GetLength(0); k++)
        {
            int kOffset = k * result2.GetLength(1);

            for (int m = 0; m < result2.GetLength(1); m++)
            {
                result3[iOffset + j, kOffset + m] = result1[i, j] + "," + result2[k, m];
                Console.WriteLine(result3[iOffset + j, kOffset + m]);

                if (result1[i, j] > result2[k, m])
                {
                    highestMoment[i, j] = result1[i, j];
                }
                else
                {
                    highestMoment[i, j] = result2[k, m];
                }

                if (lowestMoment[i, j] == 0
                    || lowestMoment[i, j] > highestMoment[i, j])
                {
                    lowestMoment[i, j] = highestMoment[i, j];
                }

                Console.WriteLine(highestMoment[i, j]);
                Console.WriteLine(lowestMoment[i, j]);

                counter++;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM