在 C# 中查找两个列表之间的重叠
Find overlapping between two lists in c#
我有一个计算两个列表重叠的 matlab 代码。事实上,它检查列表 H 是否在另一个列表 U 内,并在列表 H 所在的位置保留列表 U 的 window(至少一次)。事实上,U,H 是 Nx2 大小的索引列表,其中每个点都有两个值,分别是 window 的起始索引和 window 的结束索引。我在 matlab 中的代码是:
function W = getwindowsspecial(U,H)
W = [];
for i = 1:size(U,1)
if any(H(:,1)>=U(i,1) & H(:,1)<=U(i,2))
W = [W;U(i,:)];
end
end
例如你可以是:
54 86
112 217
292 325
402 451
628 664
H 可以是:
129 214
297 321
406 447
637 664
结果 W 是:
112 217
292 325
402 451
我想将该代码转换为 C#。我的列表U,H如List>>。我对应的函数如下:
static List<int> getWindows(List<List<int>> list1, List<List<int>> list2)
{
List<int> list3 = new List<int>();
for (int index = 0; index < list1[0].Count; index++) {
}
return list3;
}
我的函数应该填写什么才能像Matlab一样工作?
编辑:在这段代码中,我添加了第一个和第二个列表的限制。如何将这些限制添加到元组而不是列表列表?
List<List<int>> first = new List<List<int>>();
List<List<int>> second = new List<List<int>>();
first.Add(upFirst); first.Add(downFirst);
second.Add(upSecond); second.Add(downSecond);
getWindows(first, second);
upFirst 和 upSecond 包含第一个和第二个列表的所有左边界,downFirst 和 downSecond 包含第一个和第二个列表的所有右边界。我尝试使用以下代码来完成我的工作:
for (int index = 0; index < upFirst.Count; index++){
first.Add(new List<int> { upFirst[index], downFirst[index] });
}
for (int index = 0; index < upSecond.Count; index++){
second.Add(new List<int> { upSecond[index], downSecond[index]});
}
List<List<int>> lista = GetWindows(first, second);
这是我尝试添加为元组的方法。但是使用作为功能
@M。 Nasser Javaid 结果我得到了 NULL。我首先和第二个提供列表列表的输入是:
您可以通过针对这两个列表的几个 LINQ 查询来完成此操作。为了让它更容易阅读,我假设您有元组列表,即 List<Tuple<int, int>>
s,而不是列表列表。
var W = U.Where(u => H.Any(h => h.Item1 >= u.Item1 && h.Item1 <= u.Item2));
请注意,这会复制您的 MATLAB 示例中的逻辑,但不会给出您在示例中指定的结果。如果要复制样本,将第二个条件改为h.Item2 < u.Item2
,即从H看元素的末尾,要求严格不等式
Tuple
s 对比 List
s
如果 Tuple<int,int>
感觉很奇怪,别担心 - 它们真的很容易构造:
var t = Tuple.Create(2, 3);
根据您从何处获取数据,这应该不会很麻烦。但是,如果您的数据已经是 List<List<int>>
s,您可以轻松更改条件以使用 h[0]
而不是 h.Item1
等
我建议使用元组的主要原因是它们会更有效率,尤其是当矩阵中有很多行时 U
和 H
.
请试试这个。根据给定的场景,我们需要来自 U 的数字在 H 的范围内,因此它们是...
public void Test()
{
var listU = new List<List<int>>
{
new List<int> {54, 86},
new List<int> {112, 217},
new List<int> {292, 325},
new List<int> {402, 451},
new List<int> {628, 664}
};
var listH = new List<List<int>>
{
new List<int> {129, 214},
new List<int> {297, 321},
new List<int> {406, 447},
new List<int> {637, 664}
};
GetWindows(listU, listH);
}
static List<List<int>> GetWindows(List<List<int>> listU, List<List<int>> listH)
{
List<List<int>> list3 = new List<List<int>>();
var startingOfH = listH.First()[0];
var endOfH = listH.Last()[listH.Last().Count - 1];
foreach (var num in listU)
{
var initial = num[0];
var final = num[num.Count - 1];
if (initial > startingOfH && final < endOfH)
{
list3.Add(num);
}
}
return list3;
}
编辑:
如果您想用作 Linq,请使用此
static List<List<int>> GetWindows(List<List<int>> listU, List<List<int>> listH)
{
var startingOfH = listH.First()[0];
var endOfH = listH.Last()[listH.Last().Count - 1];
return (from num in listU
let initial = num[0]
let final = num[num.Count - 1]
where initial > startingOfH && final < endOfH
select num).ToList();
}
编辑 2:
忽略初始值
static List<List<int>> GetWindows(List<List<int>> listU, List<List<int>> listH)
{
List<List<int>> list3 = new List<List<int>>();
var startingOfH = listH.First()[0];
var endOfH = listH.Last()[listH.Last().Count - 1];
foreach (var num in listH)
{
var final = num[num.Count - 1];
if (final > startingOfH && final < endOfH)
{
list3.Add(num);
}
}
return list3;
}
如果你喜欢 Linq 那么
static List<List<int>> GetWindows(List<List<int>> listU, List<List<int>> listH)
{
var startingOfH = listH.First()[0];
var endOfH = listH.Last()[listH.Last().Count - 1];
return (from num in listH
let final = num[num.Count - 1]
where final > startingOfH && final < endOfH
select num).ToList();
}
我有一个计算两个列表重叠的 matlab 代码。事实上,它检查列表 H 是否在另一个列表 U 内,并在列表 H 所在的位置保留列表 U 的 window(至少一次)。事实上,U,H 是 Nx2 大小的索引列表,其中每个点都有两个值,分别是 window 的起始索引和 window 的结束索引。我在 matlab 中的代码是:
function W = getwindowsspecial(U,H)
W = [];
for i = 1:size(U,1)
if any(H(:,1)>=U(i,1) & H(:,1)<=U(i,2))
W = [W;U(i,:)];
end
end
例如你可以是:
54 86
112 217
292 325
402 451
628 664
H 可以是:
129 214
297 321
406 447
637 664
结果 W 是:
112 217
292 325
402 451
我想将该代码转换为 C#。我的列表U,H如List>>。我对应的函数如下:
static List<int> getWindows(List<List<int>> list1, List<List<int>> list2)
{
List<int> list3 = new List<int>();
for (int index = 0; index < list1[0].Count; index++) {
}
return list3;
}
我的函数应该填写什么才能像Matlab一样工作?
编辑:在这段代码中,我添加了第一个和第二个列表的限制。如何将这些限制添加到元组而不是列表列表?
List<List<int>> first = new List<List<int>>();
List<List<int>> second = new List<List<int>>();
first.Add(upFirst); first.Add(downFirst);
second.Add(upSecond); second.Add(downSecond);
getWindows(first, second);
upFirst 和 upSecond 包含第一个和第二个列表的所有左边界,downFirst 和 downSecond 包含第一个和第二个列表的所有右边界。我尝试使用以下代码来完成我的工作:
for (int index = 0; index < upFirst.Count; index++){
first.Add(new List<int> { upFirst[index], downFirst[index] });
}
for (int index = 0; index < upSecond.Count; index++){
second.Add(new List<int> { upSecond[index], downSecond[index]});
}
List<List<int>> lista = GetWindows(first, second);
这是我尝试添加为元组的方法。但是使用作为功能
@M。 Nasser Javaid 结果我得到了 NULL。我首先和第二个提供列表列表的输入是:
您可以通过针对这两个列表的几个 LINQ 查询来完成此操作。为了让它更容易阅读,我假设您有元组列表,即 List<Tuple<int, int>>
s,而不是列表列表。
var W = U.Where(u => H.Any(h => h.Item1 >= u.Item1 && h.Item1 <= u.Item2));
请注意,这会复制您的 MATLAB 示例中的逻辑,但不会给出您在示例中指定的结果。如果要复制样本,将第二个条件改为h.Item2 < u.Item2
,即从H看元素的末尾,要求严格不等式
Tuple
s 对比 List
s
如果 Tuple<int,int>
感觉很奇怪,别担心 - 它们真的很容易构造:
var t = Tuple.Create(2, 3);
根据您从何处获取数据,这应该不会很麻烦。但是,如果您的数据已经是 List<List<int>>
s,您可以轻松更改条件以使用 h[0]
而不是 h.Item1
等
我建议使用元组的主要原因是它们会更有效率,尤其是当矩阵中有很多行时 U
和 H
.
请试试这个。根据给定的场景,我们需要来自 U 的数字在 H 的范围内,因此它们是...
public void Test()
{
var listU = new List<List<int>>
{
new List<int> {54, 86},
new List<int> {112, 217},
new List<int> {292, 325},
new List<int> {402, 451},
new List<int> {628, 664}
};
var listH = new List<List<int>>
{
new List<int> {129, 214},
new List<int> {297, 321},
new List<int> {406, 447},
new List<int> {637, 664}
};
GetWindows(listU, listH);
}
static List<List<int>> GetWindows(List<List<int>> listU, List<List<int>> listH)
{
List<List<int>> list3 = new List<List<int>>();
var startingOfH = listH.First()[0];
var endOfH = listH.Last()[listH.Last().Count - 1];
foreach (var num in listU)
{
var initial = num[0];
var final = num[num.Count - 1];
if (initial > startingOfH && final < endOfH)
{
list3.Add(num);
}
}
return list3;
}
编辑: 如果您想用作 Linq,请使用此
static List<List<int>> GetWindows(List<List<int>> listU, List<List<int>> listH)
{
var startingOfH = listH.First()[0];
var endOfH = listH.Last()[listH.Last().Count - 1];
return (from num in listU
let initial = num[0]
let final = num[num.Count - 1]
where initial > startingOfH && final < endOfH
select num).ToList();
}
编辑 2: 忽略初始值
static List<List<int>> GetWindows(List<List<int>> listU, List<List<int>> listH)
{
List<List<int>> list3 = new List<List<int>>();
var startingOfH = listH.First()[0];
var endOfH = listH.Last()[listH.Last().Count - 1];
foreach (var num in listH)
{
var final = num[num.Count - 1];
if (final > startingOfH && final < endOfH)
{
list3.Add(num);
}
}
return list3;
}
如果你喜欢 Linq 那么
static List<List<int>> GetWindows(List<List<int>> listU, List<List<int>> listH)
{
var startingOfH = listH.First()[0];
var endOfH = listH.Last()[listH.Last().Count - 1];
return (from num in listH
let final = num[num.Count - 1]
where final > startingOfH && final < endOfH
select num).ToList();
}