matlab - Find overlapping between two lists in c# -
i have got matlab code calculates overlap of 2 lists. in fact check if list h inside list u , keeps window of list u list h inside(at least 1 time). in fact u, h index list of nx2 size in every point there 2 values begining index of window , end index of window. code in matlab is:
function w = getwindowsspecial(u,h) w = []; = 1:size(u,1) if any(h(:,1)>=u(i,1) & h(:,1)<=u(i,2)) w = [w;u(i,:)]; end end
for example u be:
54 86 112 217 292 325 402 451 628 664
h be:
129 214 297 321 406 447 637 664
and result w is:
112 217 292 325 402 451
i want convert code c#. list u, h list>>. correspondant function following:
static list<int> getwindows(list<list<int>> list1, list<list<int>> list2) { list<int> list3 = new list<int>(); (int index = 0; index < list1[0].count; index++) { } return list3; }
what should fill in function in order work matlab one?
edit: in code add limits first , second list. how possible add limits tuple instead of list of lists?
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 contains left limits first , second list , downfirst , downsecond contains right limits first , second lists. tried use following code job:
(int index = 0; index < upfirst.count; index++){ first.add(new list<int> { upfirst[index], downfirst[index] }); } (int index = 0; index < upsecond.count; index++){ second.add(new list<int> { upsecond[index], downsecond[index]}); } list<list<int>> lista = getwindows(first, second);
this tried in order add tuples. using function of @m. nasser javaid got null result. input provide list of lists first , second are:
try please. according given scenario need numbers u in range of h are...
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; }
edit: if want use linq use this
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] initial > startingofh && final < endofh select num).tolist(); }
edit 2: ignore initial value
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; }
if 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] final > startingofh && final < endofh select num).tolist(); }
Comments
Post a Comment