添加脚本以查找最小值到 for 循环 c#

adding script to find min value to a for loop c#

我正在添加到我的 for 循环中,以在我从解析文本文件得到的数组中查找最小值和最大值。找到最大值很容易,但是对于最小值,我得到的值为零,而它应该是 1。这是我在循环中使用的 - (到目前为止,我只是测试获取 temp.x)

       for (int i = 0; i < lineCount; i++) {

        string line = dataLines [i];
        lineValues = line.Split (' ');

        Vector4 temp = new Vector4 ();
        Vector3 center = new Vector3 ();

        temp.x = float.Parse (lineValues [0]);

        maxvalueX = float.MinValue;
        minvalueX = float.MaxValue;

        if (temp.x > maxvalueX) { maxvalueX = temp.x; } 
        if (temp.x < minvalueX) { minvalueX = temp.x; }

        temp.y = float.Parse (lineValues [1]);
            if (temp.y > maxvalueY) { maxvalueY = temp.y; }

        temp.z = float.Parse (lineValues [2]);
            if (temp.z > maxvalueZ) { maxvalueZ = temp.z; }

        temp.w = float.Parse (lineValues [3]);

        data.myData [i].Set (scaleFactor*temp.x, scaleFactor*temp.y, scaleFactor*temp.z, temp.w);
        data.myData [i].Set (scaleFactor*temp.x, scaleFactor*temp.y, scaleFactor*temp.z, temp.w);
        //int value = data.myData [i].x;

        center.x = ((maxvalueX-1)/2);
        center.y = ((maxvalueY-1)/2);
        center.z = ((maxvalueZ-1)/2);

        data.dataCenter.Set (scaleFactor*center.x, scaleFactor*center.y, scaleFactor*center.z);
    }

任何关于这里出了什么问题的想法都将不胜感激!谢谢! 仁

找到最小值的最简单方法是执行与最大值相反的操作,即

if (temp.x > maxvalueX) { maxvalueX = temp.x; } 
if (temp.x < minvalueX) { minvalueX = temp.x; } 

请记住,如果最低值可能高于 0 或最高值可能低于 0,则将最大和最小默认值设置为 0 会给您带来麻烦。在循环之前你应该像这样设置它们:

float maxvalueX = float.MinValue;
float minvalueX = float.MaxValue;

这样您就可以确定无论数字遇到什么值都会被重置。确保你处理了它们也没有价值的情况。


更新:

我已经包含了您代码的完整更新版本,并附有注释以证明我的意思:

// These should be before the loop just to avoid resetting the max/min values
float maxvalueX = float.MinValue;
float minvalueX = float.MaxValue;

  for (int i = 0; i < lineCount; i++) {

    string line = dataLines [i];
    lineValues = line.Split (' ');

    Vector4 temp = new Vector4 ();
    Vector3 center = new Vector3 ();

    temp.x = float.Parse (lineValues [0]);
    if (temp.x > maxvalueX) { maxvalueX = temp.x; } 
    if (temp.x < minvalueX) { minvalueX = temp.x; }

    temp.y = float.Parse (lineValues [1]);
        if (temp.y > maxvalueY) { maxvalueY = temp.y; }

    temp.z = float.Parse (lineValues [2]);
        if (temp.z > maxvalueZ) { maxvalueZ = temp.z; }

    temp.w = float.Parse (lineValues [3]);

    data.myData [i].Set (scaleFactor*temp.x, scaleFactor*temp.y, scaleFactor*temp.z, temp.w);
    data.myData [i].Set (scaleFactor*temp.x, scaleFactor*temp.y, scaleFactor*temp.z, temp.w);
    //int value = data.myData [i].x;
}

// I've moved these out of the loop as they only use the final max/min values
    center.x = ((maxvalueX-1)/2);
    center.y = ((maxvalueY-1)/2);
    center.z = ((maxvalueZ-1)/2);

    data.dataCenter.Set (scaleFactor*center.x, scaleFactor*center.y, scaleFactor*center.z);

试试这个

if (minvalueX  < temp.x) 
{
  valueX = temp.x;
  minvalueX = valueX;
}