vector subscipts

I have a vector of floats
std::vector<std::vector<float>> items { { 0.1, 2.1, 1.1, 3.1},{ 1.2, 2.0, 0.2, 0.4 } };
I want to change this code:
unsigned int size = items.size();
Eigen::MatrixXd m = Eigen::MatrixXd::Zero(size,size);

for (unsigned int i=0; i < size; i++) {
for (unsigned int j=0; j < size; j++) {
// generate similarity
float fd = items[i] - items[j];
int similarity = exp(-d*d / 100);
m(i,j) = similarity;
m(j,i) = similarity;
}
}
SO the idea is that I deduct from one element all others in the loop.How to do this?
You have a vector of vectors, not a plain vector of floats. In your example, the size==2, so let me "unroll" your loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
std::vector<float> foo {0.1, 2.1, 1.1, 3.1};
std::vector<float> bar {1.2, 2.0, 0.2, 0.4};
Eigen::MatrixXd m = Eigen::MatrixXd::Zero(2, 2);
float fd;
int sim;

// i=0 j=0
fd = foo - foo;
sim = ...
m(0,0) = sim;
m(0,0) = sim;

// i=0 j=1
fd = foo - bar;
sim = ...
m(0,1) = sim;
m(1,0) = sim;

// i=1 j=0
fd = bar - foo;
sim = ...
m(1,0) = sim;
m(0,1) = sim;

// i=1 j=1
fd = bar - bar;
sim = ...
m(1,1) = sim;
m(1,1) = sim;

1. Aren't there some redundancy or conflicts?

2. How should vector - vector produce a float?

3. I'm not sure what the idea is. Please explain more.
I need to explain.
I have two dimensional array
std::vector<std::vector<float>> items { { 0.1, 2.1}, {1.1, 3.1}, { 1.2, 2.0}, {0.2, 0.4 } };(is this ok?)
My goal is to calculate distances between these 4 points(array elements).
sqrt((1.1-0.1)**2+(3.1-2.1)**2)
I can use this struct for distances
struct point{float x, y;};

float fd(point &x, point &y)
{
float distance, tempx, tempy;
tempx = (x.x - y.x);
tempx = pow(tempx, 2.0f);
tempy = (x.y - y.y);
tempy = pow(tempy, 2.0f);
distance = tempx + tempy;
distance = sqrt(distance);
return distance;
}

But I need for loop that will calculate all point distances.
Why do you have a vector of vectors and not simply a vector of points, since that appears to be what you want?
Can you go one step further and convert/replace your vector of vectors with std::vector<point>?

That says much more clearly what data you have.

The x.x, x.y, y.x, y.y is confusing.
I would rename and retype the arguments of your function: float fd( point lhs, point rhs );

There are essentially three types of arguments:
* by value
* by reference
* const reference
The by reference is for modifying a variable of the caller. Your fd() does not modify.
There is debate on which is more efficient today, by value or const reference. The point is small POD (plain old data), so by value is fine.

I would not use pow(), because a square is simply tempx*tempx


You have N points and you want to fill a N*N symmetric matrix.
What is on the diagonal? fd(foo, foo). That is 0.0. We can do that separately.

You only need to calculate the upper triangle.
1
2
3
4
5
6
7
8
9
10
11
12
13
for ( size_t row = 0; row < N; ++row ) {
  for ( size_t col = row+1; col < N; ++col ) {
    // calculation
    result = ...
    m(row,col) = result;
    m(col,row) = result;
  }
}

// diagonal
for ( size_t row = 0; row < N; ++row ) {
  m(row,row) = ...
}
Keskiverto thanks.But let's solve the first step.
std:: vector<float> v {0.1, 1.1, 1.2, 0.2}
std:: vector<float> r {2.1, 3.1, 2.0, 0.4}

How to subtract v(0) from v(1) and so on(I need a loop).Same for r.Then first point for my struct will be (v(0)-v(1),r(0)-r(1))
So, you have N logical points,
with x-coordinates in vector v
and y-coordinates in vector r:
1
2
3
4
5
6
for ( size_t row = 0; row < N; ++row ) {
  for ( size_t col = row+1; col < N; ++col ) {
    point lhs { v[row], r[row] };
    point rhs { v[col], r[col] };
    float distance { fd( lhs, rhs ) };
    ...
Topic archived. No new replies allowed.