Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate Manhattan Distance using eigen library?

Tags:

c++

eigen

I have two vectors like

int main(int argc, char *argv())
{
.........
Vector3f center(0.4,0.1,0.3) ;
Vector3f point(0.1,0.2,0.7);
.......
}

How can I calculate Manhattan distance using eigen library? I am using VS2010.

like image 930
user2036891 Avatar asked Dec 11 '22 18:12

user2036891


2 Answers

This isn't hard as long as you know what Manhattan distance is (though I haven't seen the term used for 3D vectors before) - just have a look in the Eigen API doc for the relevant functions, you'll then find that the following works:

Vector3f center(0.4,0.1,0.3) ;
Vector3f point(0.1,0.2,0.7);
Vector3f diff = center - point;
float manh_dist = diff.cwiseAbs().sum();
like image 167
us2012 Avatar answered Mar 18 '23 11:03

us2012


An alternative is to observe that the Manhattan distance correspond to the L1 norm which can be obtained using the generic lpNorm method with :

manh_dist = (center-point).lpNorm<1>();

See this page for reference.

like image 30
ggael Avatar answered Mar 18 '23 13:03

ggael