如何找到二维数组所有列的最佳匹配?

发布于 2024-12-13 20:43:45 字数 446 浏览 3 评论 0原文

假设我有一个如下所示的二维数组:

________________
|10|15|14|20|30|
|14|10|73|71|55|
|73|30|42|84|74|
|14|74|XX|15|10|
----------------

正如我所示,列的大小不需要相同。

现在我需要找到每一列的最佳匹配(具有最完全相同的项目和最低的差异的那一列)。当然,我可以在 n^2 中做到这一点,但这对我来说太慢了。我该怎么做呢?

我考虑过一棵 k 维树并为每个树找到最近的邻居,但我不知道它是否好并且它会按我想要的方式工作(可能不会)。

结果例如:

  • 第​​一列很可能是第三列(只有三个不同 - 10、14、42)
  • 第二列 ->第五个(只有两个不同 - 15 和 55)

等等等等...:)

Let's say that I have a 2D array that looks like:

________________
|10|15|14|20|30|
|14|10|73|71|55|
|73|30|42|84|74|
|14|74|XX|15|10|
----------------

As I showed, the columns don't need to be same size.

Now I need to find the best matching for each column (the one that has most exactly the same items and lowest different). Of course, I could do that in n^2 but it's too slow for me. How can I do it?

I thought about a k-dimension tree and finding the closest neighbor for every one, but I don't know if it's good and it will work as I want (probably not).

Result for example:

  • First column is most likely third (only three different - 10, 14, 42)
  • Second column -> fifth (only two different - 15 and 55)

and so on and so on... :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-12-20 20:43:45

如果您知道表中的所有数字都是 2 位数字(即 10 =< x <100),则为每一列创建一个布尔数组,您将在其中标记现有数字:

bool array[5][100];
std::fill( &array[0][0], &array[0][0] + sizeof(array) , false ); // init to false
for (int i = 0; i < 5; i++)
{
  for (int j = 0; j <5; j++)
  {
     array[i][table[i][j]] = true;
  }
}

从那里应该很容易。

If you know that all the numbers in the table are 2-digit numbers (i.e. 10 =< x <100), for each column create an array of booleans where you will mark the existing numbers:

bool array[5][100];
std::fill( &array[0][0], &array[0][0] + sizeof(array) , false ); // init to false
for (int i = 0; i < 5; i++)
{
  for (int j = 0; j <5; j++)
  {
     array[i][table[i][j]] = true;
  }
}

Should be easy from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文