-4

So I have a vector of vectors type double. I basically need to be able to set 360 numbers to cosY, and then put those 360 numbers into cosineY[0], then get another 360 numbers that are calculated with a different a now, and put them into cosineY[1].Technically my vector is going to be cosineYa I then need to be able to take out just cosY for a that I specify...

My code is saying this:

for (int a = 0; a < 8; a++)
{
   for int n=0; n <= 360; n++
   {
      cosY[n] = cos(a*vectorOfY[n]);
   }
   cosineY.push_back(cosY);
 }

which I hope is the correct way of actually setting it.

But then I need to take cosY for a that I specify, and calculate another another 360 vector, which will be stored in another vector again as a vector of vectors.

Right now I've got:

for (int a = 0; a < 8; a++
{
    for (int n = 0; n <= 360; n++)
    {
       cosProductPt[n] = (VectorOfY[n]*cosY[n]);
    }
    CosProductY.push_back(cosProductPt);
 }

The VectorOfY is besically the amplitude of an input wave. What I am doing is trying to create a cosine wave with different frequencies (a). I am then calculation the product of the input and cosine wave at each frequency. I need to be able to access these 360 points for each frequency later on in the program, and right now also I need to calculate the addition of all elements in cosProductPt, for every frequency (stored in cosProductY), and store it in a vector dotProductCos[a].

I've been trying to work it out but I don't know how to access all the elements in a vector of vectors to add them. I've been trying to do this for the whole day without any results. Right now I know so little that I don't even know how I would display or access a vector inside a vector, but I need to use that access point for the addition.

Thank you for your help.

Lukali
  • 343
  • 1
  • 4
  • 15
  • By vector of vectors you mean..........a matrix? Your question is too long, and it is not clear what the specific issue here is. – JMA Dec 07 '17 at 19:36
  • I've tried the following now: cosProductPt[n] = (VectorOfY[n]*CosineY[a][n]); instead, but it didn't work. – Lukali Dec 07 '17 at 19:41
  • jafergas I am definitely struggling to get my head around it, which is why it is hard for me to even explain it... I besically want to know how to access a vector inside the vector... My vector cosineY has a vector cosY inside it, I need to calculate cosProductPt by using cosY, where cosineY[0], and then store that in another vector that is storing this vector, CosProductY[0]. – Lukali Dec 07 '17 at 19:43
  • @Lukali C++ isn't a language you can actually learn through trial and error. I'd recommend you to read [one of these books](https://stackoverflow.com/q/388242) or at least check the [documentation](http://en.cppreference.com/w/cpp/container/vector). – user0042 Dec 07 '17 at 19:46
  • You claim you have, but you don’t bother to show the definition. –  Dec 07 '17 at 19:48
  • 1
    @Lukali _"My vector cosineY has a vector cosY inside it ..."_ you cannot have a named variable inside a vector. What you probably want to have is a `std::vector> cosineY;`. – user0042 Dec 07 '17 at 19:48
  • Definitely read up on `std::vector` [link](http://www.cplusplus.com/reference/vector/vector/) and I would recommend incrementing using `< vec.size()` instead of a hard-coded number even if the size is known. I also get nervous when I see `<=` as the exit condition of your inner loop. – Justin Randall Dec 07 '17 at 20:12
  • I used radians yes. Alrite forget everything, just the first code that I given, where I push back cosY, it doesn't work. The result of 0*anything should be 0, and cos(0) is 1. Now I ain't getting 1 always when I read using a for loop. I am getting many many 0s and some 1s instead, where is my problem? I believe i figured out how to actually store and see now, but i am not getting the right output... – Lukali Dec 07 '17 at 20:36
  • Thank you for help everyone, I have figured out where my problem lays. :) – Lukali Dec 07 '17 at 21:06

1 Answers1

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);
 }

Is sound so long as cosY has been pre-allocated to contain at least 360 elements. You could

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);
 }

for example, but this hangs on to cosY longer than you need to and could cause problems later, so I'd probably scope cosY by throwing the above code into a function.

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;
}

This looks horrible, returning the vector by value, but the vast majority of compilers will take advantage of Copy Elision or some other sneaky optimization to eliminate the copying.

Then I'd do almost the exact same thing for the second step.

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;
}

But we can make a couple optimizations

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;
}

And the next is kind of an extension of the first:

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;
}

We could do the same range-based for trick for the for (int n = 0; n < NumDegrees; n++), but since we are iterating multiple arrays here it's not all that helpful.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thank you for help. I did think about this but I think it is far better if I just pass my vectors of vectors by reference, rather than return them. I think I was able to solve most of my problems after posting it, without even having to wait for help. I'm not sure If I want to confuse myself more with the for loop that has a vector in brackets... I'm barely understanding my own program at this point :(. I'll take your good programming advice as I continue to develop the program, thank you so much :) – Lukali Dec 07 '17 at 21:05
  • @Lukali "I think I was able to solve most of my problems after posting it, without even having to wait for help." Good! Best way to learn! Note: Good reading on the whole returning by value thing: https://stackoverflow.com/questions/38043319/how-does-guaranteed-copy-elision-work – user4581301 Dec 07 '17 at 22:54