How to determine if points fall above an arbitrary line?

12 Ansichten (letzte 30 Tage)
James Akula
James Akula am 12 Mai 2020
Kommentiert: James Akula am 12 Mai 2020
Hello all,
I am trying to find a way to determine if points are above or below an arbitrary line. For example, the code
numXY = randi([1, 19], 1);
lineXs = [0; sort(rand(numXY, 1) * numXY)];
lineYs = rand(size(lineXs));
Xs = rand([100, 1]) * max(lineXs);
Ys = rand(size(Xs));
figure
scatter(Xs, Ys, '.k')
hold on
plot(lineXs, lineYs, 'r')
axis tight
would produce something like
How to determine which of the points in (Xs, Ys) are above or below the red line defined by (lineXs, LineYs)?
Thank you in advance!

Akzeptierte Antwort

KSSV
KSSV am 12 Mai 2020
Bearbeitet: KSSV am 12 Mai 2020
Read about inpolygon.
numXY = randi([1, 19], 1);
lineXs = [0; sort(rand(numXY, 1) * numXY)];
lineYs = rand(size(lineXs));
Xs = rand([100, 1]) * max(lineXs);
Ys = rand(size(Xs));
idx = inpolygon(Xs,Ys,[0 ; lineXs ; max(lineXs)],[ 0 ; lineYs ; 0]) ;
figure
% scatter(Xs, Ys, '.k')
hold on
plot(lineXs, lineYs, 'r')
plot(Xs(idx),Ys(idx),'*k')
plot(Xs(~idx),Ys(~idx),'*r')
  1 Kommentar
James Akula
James Akula am 12 Mai 2020
Thanks. Inpolygon is a nice answer. In the "real world" I might have to select values lower than zero for the lower edge--I will see if -inf works with inpolygon--but at the least, just picking something lower than the lowest Y would do.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by