Solved

Euclidean Distance list of lists c#

Posted on 2011-03-01
2
815 Views
Last Modified: 2012-05-11
I am computing the euclidean distance between 2 lists of lists.
In the code that i provided they both have the same number of ROWS.
But what if they don't??
What can i do then?
Suppose that tab2 has 5 inner lists instead of 3:
 List<List<double>> tab2 = new List<List<double>>() 
    {
     new List<double>() {3.8, 7.3, 2.1},
     new List<double>() {3.3, 4.0, 5.5},
     new List<double>() {4.2, 3.2, 4.2},
     new List<double>() {4.2, 3.2, 4.2},
new List<double>() {4.2, 3.2, 4.2}
    };

Open in new window

static void Main(string[] args)
  {
   List<List<double>> tab1 = new List<List<double>>() 
    {
     new List<double>() {3.3, 4.0, 5.5},
     new List<double>() {4.2, 3.2, 4.2},
     new List<double>() {3.8, 7.3, 2.1}
    };
   List<List<double>> tab2 = new List<List<double>>() 
    {
     new List<double>() {3.8, 7.3, 2.1},
     new List<double>() {3.3, 4.0, 5.5},
     new List<double>() {4.2, 3.2, 4.2}
    };
 
   double sum2 = 0.0;
   for (int idx1 = 0; idx1 < 3; ++idx1)
    for (int idx2 = 0; idx2 < 3; ++idx2)
    {
     double delta = (tab1 [idx1][idx2] - tab2[idx1][idx2]);
     sum2 += (delta * delta);
    }
 
   Console.WriteLine("Distance: {0}", Math.Sqrt(sum2));
   Console.ReadKey();
  }

Open in new window

0
Comment
Question by:dadadude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 35013504
I'm not quite sure what your lists represent. I suggest these are coordinates is some space.
In a plane we have distance between two points p = (p1, p2) and q = (q1, q2)  (each point has two coordinates):
dist = SQRT((p1-q1)^2 + (p2-q2)^2)

In 3D (p = (p1, p2,p3) and q = (q1, q2, q3):

dist = SQRT((p1-q1)^2 + (p2-q2)^2 + (p3 - q3)^2)

Really, innert list for me represents a point with 3D coordinates. So, distance between two inner lists of tab1 and tab2 has sense. Difference between lists tab1 and tab2 - a bit strange. Basicly, this is just a sum of all distances between separate points. In this case you may run loops with different limits, in your case 3 and 5 correspondingly. But I'm not sure about interpretation...

Also, as I pointed above, distance between two point is a square root of a sum of individual coordinate  deltas. Therefore you might need to get SQRT before summing distances for individual points...

OK, I might misunderstand your task...






List<List<double>> tab1 = new List<List<double>>(); 
               tab1.Add(new List<double>(new double[]{3.3d,4.0d,5.5d}));
               tab1.Add(new List<double>(new double[]{4.2,3.2,4.2}));
               tab1.Add(new List<double>(new double[]{3.8,7.3,2.1}));
 
               List<List<double>> tab2 = new List<List<double>>();
               tab2.Add(new List<double>(new double[]{3.8, 7.3, 2.1}));
               tab2.Add(new List<double>(new double[]{3.3, 4.0, 5.5}));
               tab2.Add(new List<double>(new double[]{4.2, 3.2, 4.2}));
               tab2.Add(new List<double>(new double[]{4.2, 3.2, 4.2}));
               tab2.Add(new List<double>(new double[]{4.2, 3.2, 4.2}));


             
               double sum2 = 0.0;
               for (int idx1 = 0; idx1 < tab1[0].Count; ++idx1){ //all list in tab1 have same count
                   for (int idx2 = 0; idx2 < tab2[0].Count; ++idx2) //all list in tab2 have same count
                    {
                     double delta = (tab1 [idx1][idx2] - tab2[idx1][idx2]);
                     sum2 += (delta * delta);
                    }
                }

Open in new window

0
 

Author Comment

by:dadadude
ID: 35014886
Thank you for your reply.
But my problem with the size of the list you know. And i don't think that this can solve the problem because in:
double sum2 = 0.0;
               for (int idx1 = 0; idx1 < tab1[0].Count; ++idx1){ //all list in tab1 have same count
                   for (int idx2 = 0; idx2 < tab2[0].Count; ++idx2) //all list in tab2 have same count
                    {
                     double delta = (tab1 [idx1][idx2] - tab2[idx1][idx2]);
                     sum2 += (delta * delta);
                    }
                }

Open in new window


You are just using the first List only and not the others.
I don't know how to compute as i said earlier if i have 2 lists of different size.
Should i add an empty list like:
I add a new list or call it dimension but with 0.0

 like i  pass in both lists to a routine that looked to compute the maximum size of the two.  For example if you passed in tab1 and tab2 that were 3x3 and 4x4 respectivily, the routine would create two 4x4 items and use the code like I have shown earlier.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JQuery Detecting page post back 6 34
Connect Gridview column to Textbox in C# 2 55
What!!!??? 5 60
Easiest intro into .NET CORE? 2 32
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question