3x3 矩阵和 3x1 向量的乘法
Multiplication of a 3x3 matrix and a 3x1 vector
我的程序要求用户输入三维双向量v和3 x 3双矩阵M,程序将打印出矩阵/向量积Mv。但是,我没有得到一个向量作为我的输出,我得到了一个标量。我不知道为什么,我已经将我的输出定义为向量。这是代码
#include <iostream>
using namespace std;
int main()
{
double v[3][1];
double M[3][3];
double Mv[3][1];
int i,j;
cout << "Enter in the components of the vector v:n";
for(i=0; i<3; i++)
{
cout << "Component " << i+1 << ": ";
cin >> v[i][0];
}
cout << "Enter in the components of the 3 x 3 matrix M:n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin >> M[i][j];
}
}
for(i=0; i<3; i++)
{
Mv[i][0]= 0.0;
for(j=0; j<3; j++)
{
Mv[i][0] += (M[i][j] * v[j][0]);
}
}
cout << "The product of Mv is: " << Mv[i][0] << endl;
return 0;
}
代码将产品打印为"1" - 如果我为两个向量的所有元素输入 1。
cout
上面放一行:
for(i=0; i<3; i++)
{
Mv[i][0]= 0.0;
for(j=0; j<3; j++)
{
Mv[i][0] += (M[i][j] * v[j][0]);
}
cout << "The product of Mv is: " << Mv[i][0] << endl;
}
你只在输出调用中打印一个值 Mv[i][0]
您需要创建一个循环来打印所有矢量元素:
而不是:
cout << "The product of Mv is: " << Mv[i][0] << endl;
做:
cout << "The product of Mv is: [";
// start with the delimiter as a ',' but we need to change it
// to ']' on the last iteration of the loop.
// so the result looks something like "[1.4,2.5,0.2]"
char delimiter = ',';
for(i=0; i<3; i++)
{
if (i == 2)
{
// on last loop iteration change delimiter to ']'
delimiter = ']'
}
cout << Mv[i][0] << delimiter;
}
cout << endl;
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 3x3 矩阵和 3x1 向量的乘法