0

im trying to explain the problem i have. I need a 2-d matrix which contains 233x233 row and columns.

for(int  i  =  0; i < dimension;i++)
   for(int  j  = 0 ; j < dimension;j++)


     distance3 = sqrt(pow((apointCollection2[j].x - apointCollection[i].x1), 2) + pow((apointCollection2[j].y - apointCollection[i].y1), 2));
        if (distance3 < Min)
        {
            Min = distance3;
            station = busStation;

        }
        distance2 = sqrt(pow((apointCollection2[j].x - apointCollection[i].x2), 2) + pow((apointCollection2[j].y - apointCollection[i].y2), 2));
        if (distance2 < Min2)
        {
            Min2 = distance2;
            station1 = busStation;

        }

So i find the minimum distance and two stations with minimum distance. The first station(station) corresponds to row and the second one (station1) corresponds to column. Then i need to increment the number of people these(can be called route) has.

Then i need to find the station and station1 after the second iteration and if they are the same i need just to increment people and not add the same stations to the vector.

Or another variant i thought I creat a 2-d vector with 233x233 and 0 values in each cell.

vector< vector<int> > m;
cout << "Filling matrix with test numbers.";
m.resize(233);  
for (int i = 0; i < 233; i++)
{
    m[i].resize(233); 
    for (int j = 0; j < 233; j++)
    {

    }
}

After the loop above i decided to create the following where i find the min distance : Here i want to increment somehow:

m[station][station1] = person;
    if (find(m.begin(), m.end(), station, station1))
    {
        person++;
    }
    else
    {
        m[station][station1] = person;
    }

I have an error in "find" because there is no instance of function template.Another problem i don't add values to vector but here also a mistake when i want to add.

This should be done very easy just need to find out the logic i should follow.

Thanks in advance

  • Find might be easier if you treat your 2D matrix as a 1D matrix. – erip Apr 18 '16 at 10:47
  • Use single vector to keep a matrix. [Here](http://stackoverflow.com/a/15799557/3344612) an example of the implementation. This will make your life easier. – Teivaz Apr 18 '16 at 10:51
  • btw, pow(x, 2) is much much slower than x*x – Exceptyon Apr 18 '16 at 11:01
  • @Exceptyon You have any source on that? I was pretty sure the compiler simplified that away. – Jonathan Mee Apr 18 '16 at 11:02
  • 1
    @JonathanMee http://coliru.stacked-crooked.com/a/71bcf654940d654d shows about 5x slower, ofc depends on compiler and optimizations – Exceptyon Apr 18 '16 at 11:09
  • @Exceptyon So this is interesting. I copy your code into Visual Studio 2015 and run in release, the times vary slightly, sometimes `x * x` is faster, sometimes `pow(x, 2)` is faster. But on gcc even with -O3 `x * x` is always faster. I guess that optimization only happens on Visual Studio :( – Jonathan Mee Apr 18 '16 at 11:27
  • If you want us to be able to help with this problem you need to explain what `apointCollection2[j].x` and `apointCollection[j].x1` are, cause what it initially sounded like was that you were trying to recreate the [Traveling Salesman Problem](https://simple.wikipedia.org/wiki/Travelling_salesman_problem), but now I don't think that is it, but I have no idea what this is... – Jonathan Mee Apr 18 '16 at 11:41
  • Nope TSP problem , i know how to create it and it isnt the same. I just need to create a matrix where already are 233x233 rows and colums ( each column and each row represent stations). When i find the min distance , i need to increment in the rigth cell the number of people and. That's it. What you ask me is just coordinates – Misha Ostapchuk Apr 18 '16 at 12:10
  • As a side note: `std::vector > m(233, std::vector(233));` will create your matrix of desired dimensions with no additional resizing. – bipll Apr 18 '16 at 12:54

0 Answers0