To'g'ri oraliqdagi manzilni oling

Menda intervallar ro'yxati bor:

Intervals = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}}

va o'lchovlar ro'yxati:

Measurements = {0.1`, 3.2`, 2.5`, 1.4`, 5.8`, 5.9`}

Endi o'lchashni o'z ichiga olgan intervalni o'rnini olishni istayman. Quyidagi kod mukammal ishlaydi:

Map[(temp = #; 
   [email protected]@
     Position[Map[(IntervalMemberQ[Interval[#], temp]) &, Intervals], 
      True]) &, Measurements]

Shunga qaramay, Nestd Map qurilishini juda mazali emas deb hisoblayman. Ushbu muammoni yanada chiroyli tarzda hal qilish uchun biron bir fikr bormi?

4

7 javoblar

IntervalMemberQ accepts lists as arguments (either):

IntervalMemberQ[Interval /@ Intervals, #] & /@ Measurements//Position[#, True] &//#[[All, 2]] &
2
qo'shib qo'ydi
+1 bo'lishi mumkin, lekin Flatten [Position [IntervalMemberQ [Interval/@ Intervals, #], True] va/@ Measurements]
qo'shib qo'ydi muallif Patrick Johnmeyer, manba
Last /@ Position[
  Thread[IntervalMemberQ[Interval /@ Intervals, #]] & /@ Measurements,
   True]
2
qo'shib qo'ydi
Bu erda taklif qilingan turli xil echimlar bo'yicha bir nechta testlardan so'ng (Men Algohi, kguler, ekt .. taklif qilingan echimlarni shaxsan sinab ko'rdim). Sizning yechimingiz yaxshi deb o'ylayman. Haqiqatan ham, bu yanada mustahkam va quyidagi muammolarni hal qiladi: Intervals = {{0, 1}, {1, 2}, {0, 2}, {1,5, 2}}; o'lchovlari = {1, 2, 1.5}; O'zim uchun: Yourfun [Intervals, {#}] va/@ Measurements Ko'p inklüzyonlar holatida yanada aniq bo'lishi uchun.
qo'shib qo'ydi muallif user13407, manba
doedalos @ rahmat
qo'shib qo'ydi muallif chinnagaja, manba

Faqat intervallarni tartiblashtirilgan deb hisoblaymiz.

intervals = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}};
measurements = {0.1`, 3.2`, 2.5`, 1.4`, 5.8`, 5.9`};

Map[Function[m,
  [email protected][First /@ intervals, m > # &]],
 measurements]

{1, 4, 3, 2, 6, 6}

yoki

Map[Function[m,
  Count[First /@ intervals, _?(m > # &)]],
 measurements]

{1, 4, 3, 2, 6, 6}

1
qo'shib qo'ydi
f1 = Function[{k},  Min[Length[#],
                        1 + LengthWhile[ Interval /@ #, ! IntervalMemberQ[#, k] &]]] /@ #2 &

f1[intervals, measurements]
(* {1, 4, 3, 2, 6, 6} *)

yoki

Function[{is, ms},  Min[Length[is], 
                        1 + LengthWhile[Times @@@ Subtract[is, #], Positive]] & /@ ms];
f2[intervals, measurements]
(* {1, 4, 3, 2, 6, 6} *)
1
qo'shib qo'ydi

Turli xil (faqat yopiq uchun):

intervals = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}};
measurements = {0.1`, 3.2`, 2.5`, 1.4`, 5.8`, 5.9};

Shunday qilib,

fun[i_, m_] := Pick[i, Sign[# - m] == {-1, 1} & /@ i]
rul = Thread[intervals -> Range[[email protected]]];

keyin,

(Join @@ fun[intervals, #] & /@ measurements) /. rul

hosil bo'ladi

(*{1, 4, 3, 2, 6, 6}*)
1
qo'shib qo'ydi
IntervalMemberQ[Interval /@Intervals, #] & /@ Measurements //Position[#, True] &//#[[All, 2]] &

Bu erda @Aisamu misolini ko'rib chiqayapman va uni biroz aniq qilish uchun postfiksni kamaytiraman.

 (#[[All,2]]&)@(Position[Map[IntervalMemberQ[Interval /@Intervals,#]&,Measurements],True])
1
qo'shib qo'ydi
Flatten[Position[Intervals, x_ /; IntervalMemberQ[Interval[x], #], 
    2] & /@ Measurements]
1
qo'shib qo'ydi