Find matrix (meshgrid) indices near plotted vector (points)

조회 수: 3 (최근 30일)
Addison Collins
Addison Collins 2021년 7월 2일
댓글: Addison Collins 2021년 7월 3일
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
Addison Collins
Addison Collins 2021년 7월 2일
I managed to get something close. It isn't as symetric as I'd like thought.
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);
P = [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;
PQ = [xvals(:) yvals(:)];
k = dsearchn(P,PQ)
% Plot
figure
scatter(X(:),Y(:),'red'); hold on;
scatter(X(k),Y(k),'green')
% scatter(X(k),Y(k),'blue')
title('Grid of data and intersecting line')
xlabel('mm')
ylabel('mm')
line(xvals,yvals)
grid on
axis equal
Yazan
Yazan 2021년 7월 3일
What if you sort the Y-axis data points at each X-axis point based on the difference with respect to yvals?

댓글을 달려면 로그인하십시오.

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 7월 3일
편집: Scott MacKenzie 2021년 7월 3일
If by symmetry you mean fewer points but closer to the line, then reduce the number of query points, or points in the line:
xvals = linspace(-5,5,1*n); % instead of 5*n
Then, use dsearchn like this:
P = [X(:) Y(:)];
PQ = [xvals' yvals'];
k = dsearchn(P, PQ);
The points you are looking for are in k, as shown below:
% plot grid points closest to query points
hold on
plot(P(k,1), P(k,2),'or', 'markerfacecolor', 'r');
Here's the result with only n/2 points in the line:
You could also try using a finer granularity in the grid, but I haven't explored that.
  댓글 수: 1
Addison Collins
Addison Collins 2021년 7월 3일
Thanks Scott, I didn't realize of changing the number of points going into PQ would have this effect. The grid of data I am going to use this detection method for is somewhat set already. This was a "bench test" script, if you will.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polar Plots에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by