【问题标题】:C++ get column vector from a MatrixC++ 从矩阵中获取列向量
【发布时间】:2014-12-29 19:12:08
【问题描述】:

我在我的代码中定义了一个这样的矩阵

*matrix1 = new int[*matrix1rows * *matrix1columns];
*matrix2 = new int[*matrix2rows * *matrix2columns];

现在我需要将矩阵的列保存为单个向量以将两个矩阵相乘。 例如,如果我有这样的矩阵:

1 2 3 4
5 6 7 8
9 1 2 3

例如,我想获取向量中的每一列

1
5
9

我已经创建了一个循环来将行作为向量获取,但现在我需要对列进行相同的操作,但我不知道该怎么做。

这是我获取行向量的循环。但现在我需要获取 matrix2 的列向量以将其与 matrix1 的行向量相乘。我不能改变矩阵的定义。

for (int j = 0; j < matrix1rows; j++) {
  for (int i = 0; i < matrix1columns; i++) {
      printf("%i ", matrix1[(j * matrix1columns) + i]);
  }
}

如何通过循环获取矩阵的列?

【问题讨论】:

    标签: c++ arrays matrix vector


    【解决方案1】:

    将行标签与列标签互换,如下所示:

    for (int i = 0; i < matrix1columns; i++) {
       for (int j = 0; j < matrix1rows; j++) {
          printf("%i ", matrix1[(j * matrix1columns) + i]);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 2014-05-11
      • 1970-01-01
      • 2023-03-17
      • 2017-09-27
      相关资源
      最近更新 更多