【问题标题】:c++ method to order a matrix by elements in a column (the same of sortrows in MATLAB)c++ 方法按列中的元素对矩阵进行排序(与 MATLAB 中的 sortrows 相同)
【发布时间】:2022-04-05 20:18:41
【问题描述】:

我正在寻找一种 c++ 方法,该方法能够按特定列中值的降序对矩阵的行进行排序。

例如,如果这是我的输入:

matrix = [1,2,3;
          2,4,1;
          0,5,2]

方法(矩阵,3)的调用应该提供以下输出:

outputMatrix = [1,2,3;
                0,5,2;
                2,4,1]

在 MATLAB 中,这可以通过调用函数来完成:

outputMatrix = sortrows(matrix,3).

c++ 呢? (在这两种情况下,3 都是列的索引)。

此外,在我正在工作的脚本中,矩阵被定义为向量的向量:std::vector<std::vector<double> > matrix

[编辑]我再添加一个例子:

input = [4,5,6;
         0,2,8;
         1,2,3;
         6,7,9]

outputMatrix = sortrows(输入,2); 第 2 列,有序,是:9,8,6,3;所以我必须对行进行排序并复制前两列的元素(分别为 0 和 1)。

 outputMatrix = [6,7,9;
                 0,2,8;
                 4,5,6;
                 1,2,3]

我在这里报告我写的方法,但我不知道这是否是一个快速的方法:

std::vector<std::vector<double> > sortrows(std::vector<std::vector<double> > matrix,int col){
    int length = matrix[col].size();
    std::vector<std::vector<double> > output(3,std::vector<double>(length*length));
    output[col] = matrix[col];
    std::sort(output[col].begin(),output[col].end(),std::greater<double>());
    for (int i = 0; i < length*length;i++){
        int index = 0;
        while(output[col][i]!=matrix[col][index]){index++;}
        output[0][i]=matrix[0][index];
        output[1][i]=matrix[1][index];
        matrix[2][index] = -1;
    }
    return output;
}

【问题讨论】:

  • 你知道如何实现一些简单的东西,比如一个 int 数组吗?你对std::sort 满意吗?
  • 感谢您的帮助,无论如何我是 C++ 的新手,我已经阅读了有关仅对向量而非矩阵使用排序的信息。特别是我不知道如何使用条件函数。
  • 如果您希望有人为您编写函数,请询问同事。如果你想学习自己写,我建议你先用铅笔和纸在一个小的整数数组上实现Bubble Sort。你必须从简单的开始,爬到复杂的。

标签: c++ sorting matrix


【解决方案1】:

类似以下(在 C++11 中),根据特定列对矩阵行进行排序

void sortrows(std::vector<std::vector<double>>& matrix, int col) {    
    std::sort(matrix.begin(),
              matrix.end(),
              [col](const std::vector<double>& lhs, const std::vector<double>& rhs) {
                  return lhs[col] > rhs[col];
              });
}

另请注意,此代码更改了原始矩阵,而不是返回一个新矩阵。

非 C++11 版本:

class Compare {
public:
    Compare(int col) : col_(col) {}
    bool operator()(std::vector<double>& lhs, std::vector<double>& rhs) {
        return lhs[col_] > rhs[col_];
    }
private:
    int col_; 
};

void sortrows(std::vector<std::vector<double>>& matrix, int col) {    
    std::sort(matrix.begin(), matrix.end(), Compare(col));
}

【讨论】:

  • 感谢您的帮助,反正我是 C++ 的新手。我不明白这是否是一种方法,特别是我必须做什么才能使用此代码?
  • 再次感谢您的代码。但这并不能满足我的需要(如上面的示例所示)。也许我无法以正确的方式使用该方法。 ;-)
  • 正如我所说,它改变了原始矩阵,你可以先复制矩阵,然后变异复制的矩阵。
  • 是的,我明白这一点。但我要说的是:此方法不会按照特定列中值的降序对矩阵的行进行排序。它似乎对列进行了排序。
  • @Mattia,请再给我一个样品,因为我已经测试了您提供的样品。最好展示一下你是如何使用它的。请注意,在您给出的示例中,您应该调用 sortrows(matrix, 2),因为在 C++ 中索引从 0 开始
【解决方案2】:
template<typename T>
void SortRows(std::vector<std::vector<T>>& inputVector,int col)
{
    bool stay{true};
    for (int ii = 0; ii< inputVector.size();ii++)
    {
        if(stay)
        {
            stay = false;
            for(int jj=0;jj<inputVector.size()-1;jj++)
            {
                if(inputVector[jj][col] < inputVector[jj+1][col])
                {
                    inputVector[jj].swap(inputVector[jj+1]);
                    stay = true;
                }
            }
        }
        else
        {
            break;
        }
    }
}

这是一种冒泡排序算法,可以解决您的问题。

请注意,由于 '' 进行升序。

如果您有任何犹豫,请再次询问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多