Count elements of a list
n = 100;
data = Table[{RandomReal[{0, 2}], RandomReal[{-1, 1}]}, {i, 0, n}];
y1[x_] = x - 1;
y2[x_] = 1 - x;
Plot[{y1[x], y2[x]}, {x, -0.5, 2.5}, Epilog -> Point[data]]
selection =
Select[data, #[[2]] > y1[#[[1]]] && #[[2]] > y2[#[[1]]] &];
Plot[{y1[x], y2[x]}, {x, -0.5, 2.5}, Epilog -> Point[selection]]
Length[selection]
30
this code works for selection in all 4 sectors
s = Table[
Select[data, #[[2]]~op1~y1[#[[1]]] && #[[2]]~op2~y2[#[[1]]] &],
{op1, {Greater, Less}}, {op2, {Greater, Less}}] // Flatten[#, 1] &;
Length /@ s
{27, 25, 25, 24}
data2 = GatherBy[data, Sign @ {#[[2]] - m1 (#[[1]] - 1), #[[2]] - m2 (#[[1]] - 1)} &];
Length /@ data2
(* {29, 20, 24, 28} *)
ListPlot[data2, AspectRatio -> 1, Epilog -> ls[[1]], BaseStyle -> PointSize[Large]]
To get just the counts, you can also use Count
with Sign
:
Count[Sign[{#2 - m1 (# - 1), #2 - m2 (# - 1)}] & @@@ data, #] & /@ Tuples[{1, -1}, 2]
(* {24, 20, 29, 28} *)
Count[Sign[{#2 - y1 /. x -> #, #2 - y2 /. x -> #}] & @@@ data, #] & /@
Tuples[{1, -1}, 2]
(* {24, 20, 29, 28} *)
{Row@{#},
Count[Sign[{#2 - m1 (# - 1), #2 - m2 (# - 1)}] & @@@ data, #]} & /@
Tuples[{1, -1}, 2] //
TableForm[#, TableHeadings -> {None, {"Signs", "Count"}}] &
or, with UnitStep
:
Count[UnitStep[{#2 - m1 (# - 1), #2 - m2 (# - 1)}] & @@@ data, #]& /@Tuples[{1, 0}, 2]
(* {24, 20, 29, 28} *)
Count[UnitStep[{#2 - y1 /. x -> #, #2 - y2 /. x -> #}] & @@@ data, #] & /@
Tuples[{1, 0}, 2]
(* {24, 20, 29, 28} *)
You can refine your list of data usind Cases
s1 = Cases[data, l_List /;
(l[[1]] > 1) &&
(l[[2]] > m2*(l[[1]] - 1)) &&
(l[[2]] < m1*(l[[1]] - 1))
];
There are conditional pattern l_List
which will be applied for elements of your data
list. As result, you obtain following:
Show[
ListPlot@data,
ListPlot[s1, PlotStyle -> Red],
Plot[{y1, y2}, {x, -2, 2}, PlotStyle -> {{Magenta, Dashed, Thick}},
AspectRatio -> 1]]