Find matrix (meshgrid) indices near plotted vector (points)
Show older comments
Hi,
I am attempting to grab several datapoints that are near a vector of points (represented by a line in the plot). I am unsure how to accomplish this with k = dsearchn(P,PQ) or Idx = knnsearch(X,Y,Name,Value). Ideally, the indices of the datapoints very close to the line's datapoints will be pulled and I can reference them later for some calculations.
clc;clear;close all
% Plot data to be used in aspiration calculation
n = 35;
X = linspace(-5,5,n);
Y = linspace(-5,5,n);
[X,Y] = meshgrid(X,Y);
% Create points for line
x1=-1.522; x2=1.847;
y1=-0.761; y2=0.4344;
coefficients = polyfit([x1, x2], [y1, y2], 1);
a = coefficients (1);
b = coefficients (2);
xvals = linspace(-5,5,5*n); % x values that will be used to calculate y values. Can be
yvals = a*xvals+b;
% Plot
figure
scatter(X(:),Y(:),'red')
% scatter(X(k),Y(k),'blue')
title('Grid of data and intersecting line')
xlabel('mm')
ylabel('mm')
line(xvals,yvals)
grid on
axis equal

2 Comments
Addison Collins
on 2 Jul 2021
Yazan
on 3 Jul 2021
What if you sort the Y-axis data points at each X-axis point based on the difference with respect to yvals?
Accepted Answer
More Answers (0)
Categories
Find more on Polar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

