How to extract the data using inpolygon function ?

Hi everyone,
May some one help me here ...
I have data in three colums. (1) x-coordinate; (2) y-coordinate; and (3) z-value (parametere) of data length (55000 data points: I reduce the file size due to limit of 5 MB) I required to extarct the z-values lying inside the polygon.
My scripit did not work for the above mentioned purpose. May someone help me how to handle this data and simple script attched here.
clear all
clc
data=xlsread('Mc_catalog.xlsx');
for i=1:length(data)
x = data(:,1) ; y = data(:,2) ; z = data(:,3 ) ;
xVertices = [-116.8, -116.7, -116.45, -116.7];
yVertices = [33.55, 33.40, 33.70, 33.85];
idx(i) = inpolygon(x(i), y(i), xVertices, yVertices);
iwant(i) = [ z(idx )] ;
end
Thank you.

 Respuesta aceptada

Consult the inpolygon documentation page. You can probably do this without the for loop, as inpolygon can query a vector of points at once.
Here is an example from the linked page.
% Define polygon
L = linspace(0,2*pi,6);
xv = cos(L)';
yv = sin(L)';
% Define points to query
xq = randn(250,1);
yq = randn(250,1);
% Find points inside polygon
[in,on] = inpolygon(xq,yq,xv,yv);
% Visualize results
figure
plot(xv,yv) % polygon
axis equal
hold on
plot(xq(in),yq(in),'r+') % points inside
plot(xq(~in),yq(~in),'bo') % points outside
hold off

3 comentarios

I even try with out the for loop but the output show empty iwant(see my code).
Yes, I follow this exmaple but this not serve for my purpose may you modify
clear all
clc
data=xlsread('Mc_catalog.xlsx');
x = data(:,1) ; y = data(:,2) ; z = data(:,3 ) ;
xVertices = [-116.8, -116.7, -116.45, -116.7];
yVertices = [33.55, 33.40, 33.70, 33.85];
idx = inpolygon(x, y, xVertices, yVertices);
iwant = [ z(idx )] ;
This syntax should work. Perhaps data does not have the values you think it does?
Here's how I might visualize the results.
data = readmatrix('data_new.xlsx');
x = data(:,1) ; y = data(:,2) ; z = data(:,3 ) ;
xVertices = [-116.8, -116.7, -116.45, -116.7];
yVertices = [33.55, 33.40, 33.70, 33.85];
idx = inpolygon(x, y, xVertices, yVertices);
iwant = z(idx);
% Visualize
plot3(xVertices,yVertices,zeros(size(xVertices)))
hold on
scatter3(x(idx),y(idx),iwant)
hold off
aa
aa el 25 de En. de 2021
Thank you very much this serve for my purpose.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

aa
el 25 de En. de 2021

Comentada:

aa
el 25 de En. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by