所以我有一个矢量类型为double的向量。我基本上需要能够将360数字设置为舒适,然后将这些360数字放入cosineY [0],然后获得另外360个数字,这些数字现在用不同的a计算,并将它们放入cosineY [1]。技术上我的矢量将是cosineYa然后我需要能够取出我指定的舒适......
我的代码是这样说的:
for (int a = 0; a < 8; a++)
{
for int n=0; n <= 360; n++
{
cosY[n] = cos(a*vectorOfY[n]);
}
cosineY.push_back(cosY);
}
我希望这是实际设置它的正确方法。
然后我需要对我指定的内容感到舒适,并计算另一个另外的360矢量,它将作为矢量矢量再次存储在另一个矢量中。
现在我有:
for (int a = 0; a < 8; a++
{
for (int n = 0; n <= 360; n++)
{
cosProductPt[n] = (VectorOfY[n]*cosY[n]);
}
CosProductY.push_back(cosProductPt);
}
VectorOfY实际上是输入波的幅度。我正在做的是尝试创建不同频率的余弦波(a)。然后我计算每个频率的输入和余弦波的乘积。我需要能够在程序中稍后为每个频率访问这360个点,现在我还需要计算cosProductPt中所有元素的添加,对于每个频率(存储在cosProductY中),并将其存储在向量中dotProductCos [A]。
我一直在尝试解决这个问题,但我不知道如何访问矢量矢量中的所有元素来添加它们。我一直试图这样做一整天没有任何结果。现在我知道的很少,甚至不知道如何在向量中显示或访问向量,但我需要使用该访问点进行添加。
感谢您的帮助。
答案 0 :(得分:0)
for (int a = 0; a < 8; a++)
{
for int n=0; n < 360; n++) // note traded in <= for <. I think you had an off by one
// error here.
{
cosY[n] = cos(a*vectorOfY[n]);
}
cosineY.push_back(cosY);
}
只要预先分配cosY
以包含至少360个元素,就会发出声音。你可以
std::vector<std::vector<double>> cosineY;
std::vector<double> cosY(360); // strongly consider replacing the 360 with a well-named
// constant
for (int a = 0; a < 8; a++) // same with that 8
{
for int n=0; n < 360; n++)
{
cosY[n] = cos(a*vectorOfY[n]);
}
cosineY.push_back(cosY);
}
例如,但是这会比你需要的时间更长到cosY
并且可能会在以后引起问题,所以我可能通过将上面的代码放到函数中来限制cosY
。< / p>
std::vector<std::vector<double>> buildStageOne(std::vector<double> &vectorOfY)
{
std::vector<std::vector<double>> cosineY;
std::vector<double> cosY(NumDegrees);
for (int a = 0; a < NumVectors; a++)
{
for int n=0; n < NumDegrees; n++)
{
cosY[n] = cos(a*vectorOfY[n]); // take radians into account if needed.
}
cosineY.push_back(cosY);
}
return cosineY;
}
这看起来很糟糕,按值返回vector
,但绝大多数编译器会take advantage of Copy Elision或其他一些偷偷摸摸的优化来消除复制。
然后,我在第二步几乎完成了同样的事情。
std::vector<std::vector<double>> buildStageTwo(std::vector<double> &vectorOfY,
std::vector<std::vector<double>> &cosineY)
{
std::vector<std::vector<double>> CosProductY;
for (int a = 0; a < numVectors; a++)
{
for (int n = 0; n < NumDegrees; n++)
{
cosProductPt[n] = (VectorOfY[n]*cosineY[a][n]);
}
CosProductY.push_back(cosProductPt);
}
return CosProductY;
}
但我们可以进行一些优化
std::vector<std::vector<double>> buildStageTwo(std::vector<double> &vectorOfY,
std::vector<std::vector<double>> &cosineY)
{
std::vector<std::vector<double>> CosProductY;
for (int a = 0; a < numVectors; a++)
{
// why risk constantly looking up cosineY[a]? grab it once and cache it
std::vector<double> & cosY = cosineY[a]; // note the reference
for (int n = 0; n < numDegrees; n++)
{
cosProductPt[n] = (VectorOfY[n]*cosY[n]);
}
CosProductY.push_back(cosProductPt);
}
return CosProductY;
}
接下来是第一个的扩展:
std::vector<std::vector<double>> buildStageTwo(std::vector<double> &vectorOfY,
std::vector<std::vector<double>> &cosineY)
{
std::vector<std::vector<double>> CosProductY;
std::vector<double> cosProductPt(360);
for (std::vector<double> & cosY: cosineY) // range based for. Gets rid of
{
for (int n = 0; n < NumDegrees; n++)
{
cosProductPt[n] = (VectorOfY[n]*cosY[n]);
}
CosProductY.push_back(cosProductPt);
}
return CosProductY;
}
我们可以为for (int n = 0; n < NumDegrees; n++)
做同样的基于范围的技巧,但由于我们在这里迭代多个数组,所以它并没有那么有用。