计算C中的欧几里得距离矩阵 [英] Calculate Euclidean distance matrix in C

查看:72
本文介绍了计算C中的欧几里得距离矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将用MATLAB编写的代码转换为C:

I would like to convert this code which is written in MATLAB to C:

matrix = [1 2 3; 4 5 6; 7 8 10]
dis=zeros(9);
for i=1:3
    for j=1:3
        dis(i,j)=sqrt(sum (abs((matrix(i,:)-matrix(j,:))))^2);
    end
end

输出如下:

    0    9   19
    9    0   10
   19   10    0

这是我在C语言中想到的:

Here is what I came up with in C:

#include <stdio.h>
#include <math.h>

int main() {

  double distance[3][3] = {0};
  double myArray[3][3] = { {1, 2, 3}, {4 , 5, 6}, {7, 8, 9} };

  int i, j;
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {

      distance[i][j] = sqrt(pow(myArray[i] - myArray[j], 2));

    }
  }

  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {

      printf("%f ", distance[i][j]);
      if (j == 2) {
        printf("\n");
      }
    }
  }
  return 0;
}

但它显示一个空数组:

0 0 0                                                     
0 0 0                                                                                       
0 0 0

我的错误在哪里?

推荐答案

您的代码有两个问题.

  1. 我认为,矩阵的输入数据应该是 matrix = [1 2 3;4 5 6;7 8 10] ,但是输入数据在您的代码中有所不同(观察最后一个元素;赋值中的 10 在您的代码中变为 9 ).

  1. I think, your input data for the matrix is supposed to be matrix = [1 2 3; 4 5 6; 7 8 10], however input data is different in your code (observe the last element; 10 in assignment becomes 9 in your code).

我认为这些点是空间上的(如x,y和z坐标).因此,您需要第三个循环;首先是外循环中的点 point_1 = { 1, 2, 3 }, ... 等,其次是内循环中的点 ... point_2 = { 4, 5, 6} ... 等,第三个坐标代表三个坐标 x = 1,y = 2,z = 3 .

Those points, I think, are spatial (like x, y & z coordinates). So, you need a third loop; first for points in the outer loop point_1 = { 1, 2, 3 }, ... etc, second for points in the inner loop ... point_2 = { 4, 5, 6 }... etc, and a third one for three coordinates x = 1, y = 2, z = 3.

sqrt 返回一个双精度值.您最好像(int)一样将返回值强制转换为int.

sqrt returns a double. You'd better cast the returning value to int like (int).

正如@sahwahn所指出的;您可以计算距离,但永远不会保存该值.

As @sahwahn pointed; you compute the distance but never save the value.

您的嵌套循环结构可能看起来像;

Your nested loop structure may look like;

for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
        int temp = 0;

        for (k = 0; k < 3; k++) {
            temp += (int)sqrt(pow(myArray[i][k] - myArray[j][k], 2));
        }

        distance[i][j] = temp;
    }
}

顺便说一句;在空间坐标中进行真实距离计算的公式为:((坐标差的平方)的平方和)的平方根,而不是(((坐标差)的平方)的平方根的平方和)

BTW; formula for a true distance computation in spatial coordinates is: square root of (the sum of the squares of (the coordinate difference)), not the sum of (square root of (the squares of (the coordinate difference))).

由于我不确定任务,所以我坚持问题中给出的信息.从逻辑上讲,对于真正的距离计算,您的内部循环必须是;

Because I was unsure about the assignment, I stick to the info given in the question. Logically, for a true distance computation, your inner loop needs to be;

double temp = 0.0f;

for (k = 0; k < 3; k++) {
    temp += pow(myArray[i][k] - myArray[j][k], 2);
}

distance[i][j] = (int)sqrt(temp);

这篇关于计算C中的欧几里得距离矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆