- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这可能更像是一道数学题,但它最终会变成代码,所以这里...
我有一组任意数字,需要根据用户输入的最接近的相似性将其分组为固定数量的组。集合的长度和数字的值都可以变化。
这是一个示例集...
1.2
1.3
0.5
0.7
1.3
1.4
0.7
0.9
1.1
1.3
必须保留这些项目的顺序 - 最终集合不能重新排序,但可以移动。例如,最后的数字可以成为第一个数字。 这个集合被应用于一个圆,所以任何保持圆的完整性的东西都是好的。
因此,如果用户输入 4
作为所需的组数,我希望输出如下:
0 =>
1.2
1.3
1 =>
0.5
0.7
2 =>
1.3
1.4
0.7
3 =>
0.9
1.1
1.3
在更好的情况下,可以改变数字以提高它们在组中的相似度。例如,将最后两个数字移到开头...
0 =>
1.1
1.3
1.2
1.3
1 =>
0.5
0.7
2 =>
1.3
1.4
3 =>
0.7
0.9
是否有对此有用的算法?关于如何完成这样的事情有什么建议吗?
谢谢!
最佳答案
您要完成的是 Single-Linkage clustering 的特例,其中您的域是线性和圆形的。您将拥有一个距离数组,而不是距离矩阵。据推测,您的距离函数是数组中数字之差的绝对值。
这段代码绝不是最简洁、写得最好的 C#;但它确实突出了聚类算法的所有重要步骤:
简单的距离计算
class Node
{
public double Value { get; set; }
public Node NextNode { get; set; }
public Cluster Cluster { get; set; }
}
class Cluster : List<Node>
{
}
static void Main()
{
double[] values = new double[]
{
1.2,
1.3,
0.5,
0.7,
1.3,
1.4,
0.7,
0.9,
1.1,
1.3,
};
List<Node> nodes = new List<Node>();
foreach (double value in values)
{
nodes.Add(new Node { Value = value });
}
// Put each node in a cluster by itself
foreach (Node node in nodes)
{
node.Cluster = new Cluster();
node.Cluster.Add(node);
}
// Create the cirular Linked List here
// could probably use System.Collections in some way
// but using simple self written classes for clarity
for (int n = 1; n < nodes.Count; n++)
{
nodes[n - 1].NextNode = nodes[n];
}
nodes[nodes.Count - 1].NextNode = nodes[0];
// Create a sorted distance list
List<Node> sortedNodes = new List<Node>(nodes);
sortedNodes.Sort((a, b) =>
{
var aDistToNext = Math.Abs(a.Value - a.NextNode.Value);
var bDistToNext = Math.Abs(b.Value - b.NextNode.Value);
return aDistToNext.CompareTo(bDistToNext);
});
// Register each node / cluster to the output list
List<Cluster> clusters = new List<Cluster>();
foreach (Node node in nodes)
{
clusters.Add(node.Cluster);
}
// Merge clusters until the desired number is reached
int distIdx = 0;
while (clusters.Count > 4)
{
// Obtain the two next closest nodes
var nodeA = sortedNodes[distIdx];
var nodeB = nodeA.NextNode;
// Merge the nodes into a single cluster
nodeA.Cluster.AddRange(nodeB.Cluster);
// Remove the unnecessary cluster from output set
clusters.Remove(nodeB.Cluster);
nodeB.Cluster = nodeA.Cluster;
distIdx++;
}
// Print the output results
for (int n = 0; n < clusters.Count; n++)
{
Console.WriteLine("{0} =>", n);
foreach (Node node in clusters[n])
{
Console.WriteLine("\t{0}", node.Value);
}
}
}
请注意,该算法按预期工作;但是,它给出的结果与原始问题中发布的结果不同。差异是由于节点间距离相等时的模糊链接。
输出:
0 =>
0.5
0.7
1 =>
1.3
1.4
2 =>
0.7
3 =>
0.9
1.1
1.3
1.2
1.3
高级距离计算
如果您想在连接到先前集群中已经加入的节点之前优先考虑相似距离加入的节点,您可以修改排序算法以优先考虑未连接的节点:
sortedNodes.Sort((a, b) =>
{
var aDistToNext = Math.Abs(a.Value - a.NextNode.Value);
var bDistToNext = Math.Abs(b.Value - b.NextNode.Value);
var result = aDistToNext.CompareTo(bDistToNext);
if (result != 0)
return result;
else
{
var aNextDistToNext = Math.Abs(a.NextNode.Value - a.NextNode.NextNode.Value);
var bNextDistToNext = Math.Abs(b.NextNode.Value - b.NextNode.NextNode.Value);
return bNextDistToNext.CompareTo(aNextDistToNext);
}
});
这给出了预期的结果:
0 =>
0.5
0.7
1 =>
1.3
1.4
2 =>
0.7
0.9
3 =>
1.1
1.3
1.2
1.3
关于c# - 将数字列表分组为类似数字的固定集合(首选循环移位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23685557/
我想将这个无符号数:1479636484000 向右移动 7 位。这在 JavaScript 中可能吗? 两者 1479636484000 >> 7 和 1479636484000 >>> 7 返回错
鉴于以下代码: import matplotlib.pyplot as plt import numpy as np x = [1.0, 1.1, 2.0, 5.7] y = np.arange(le
我有一个低级键盘钩子(Hook),目前允许我从任何应用程序(包括游戏)中控制媒体播放器。 它通过查看捕获的特定击键来工作。 我想扩展它以查找键的组合。我可以对一些逻辑进行硬编码,但我觉得必须有一种更合
我需要一些帮助来理解这段C代码。我不知道这里的“L”和“\”是什么?请也说明一点:) #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>2
我正在查看一段代码: int result = 0 ; char byte = foo[j] for (i = 7 ; i>0 ; i--) { byte = (byte & ~0x1)>>1
我们有一个项目要求我们编写一个程序,允许用户输入一系列数字“将数字读入数组以进行进一步处理,用户通过输入负数表示他们已完成(负数不用于计算),在读取所有数字后执行以下操作,总结输入的#,计算输入的#,
锁定。有disputes about this question’s content正在解决中。它目前不接受新的答案或互动。 def menu(): choice = input("Pres
为什么如果 int x = -1 // binary: 11111111111111111111111111111111 x = x >>> 31; 我们有 000000000000000000000
我的问题其实应该很简单:我有一个玩家对象数组。(玩家[])我想要一个函数来旋转这个数组直到一个索引: public void rotateArray(Object[] array, int index
我有一个编码为 boost 动态位集的数字列表。我根据此列表中的任何数字可以采用的最大值动态选择此位集的大小。所以假设我有从 0 到 7 的数字,我只需要三位,我的字符串 0,2,7 将被编码为000
我能想到一些令人讨厌的低效方法来完成这项任务,但我想知道最好的方法是什么。 例如,我想复制一个字节中从第 3 位开始的 10 个字节,并像往常一样复制到一个指针。 有没有比一次复制一个移位字节更好的方
我正在尝试为该问题添加更多规则,并且该规则一直给我带来这种转变/减少冲突的能力,我不知道为什么会这样做,并且在过去的24小时内我一直在尝试解决问题 FuncDecl : RetTyp
This question already has answers here: Why does it make a difference if left and right shift are us
我在 Perl 中遇到这个问题已经有几天了,在搜索了无数的手册页、perldocs 和谷歌搜索了太多的搜索词之后,希望这里有人能帮助我。 我得到两个表示十六进制值的字符串,即“FFFF”,而不是 Pe
我有一个主 div,两个 div 水平并排放置在这个父 div 中。 .parent{ height: 360px; margin-top: 0px; bo
我想 float 我的元素列表并从第二个元素创建一个移动效果。 如何避免第二个 .item 之后的“清除”行为? .shift { float: right; width: 50%;
我正在使用 SSE3 优化我的代码。代码中有一点迫使我将 vector 中的所有元素移动一个元素 v[0] = 0 //v is some char* and N = v.size() for(i
.file "calcnew.c" .text .globl calcnew .type calcnew, @function calcnew:
我有一个点对象: class Point { final int x,y; ... } 因为这些点将在我的代码中到处使用/创建,所以我想开始使用 guavas 缓存。不幸的是
x = "Foo 890 bar *()" 如何将包括 "*()" 在内的小写字母“未移位”返回到 890?期望的结果: foo 890 bar 890 不需要的: x.lower() => "foo
我是一名优秀的程序员,十分优秀!