8 views (last 30 days)

Show older comments

Hello all,

I am attempting to find the location of highest datapoint density. I intend on removing datapoints that fall outside an oval and I'd like to attempt to automate the location (and size) of the oval such that it encompasses the area on the graph with the tightest distribution. This is because said data are real values and data that varies too much from this densly packed data is bad. For simplicity purposes, I uploaded only one dataset, but typically there are several. The data used in the plot is attached (clean_data.mat).

Plotting velocities against each other: Here I would like to position and size the oval such that is captures the tight band points. Currently I have to manually choose the location and size. Below, we can see that the oval is below the conecntration of points.

CODE: clean_data is attached to this post.

clc;clear;close all;

%% ------------- Remove NaN Points from Raw Data ------------------

load(['clean_data.mat'])

%% Scatter Plot ELLIPSE

figure

t = linspace(0,2*pi,5000);

theta = deg2rad(90); % vertical ellipse

a = 30; b = 15; % major and minor radii

U0=nanmean(Uvec);V0=nanmean(Vvec); % Oval center

xv = U0 + a*cos(t)*cos(theta) - b*sin(t)*sin(theta);

yv = V0 + b*sin(t)*cos(theta) + a*cos(t)*sin(theta);

plot(xv,yv); hold on;

[in,on] = inpolygon(Uvec,Vvec,xv,yv); % Points inside and on oval

plot(Uvec(in),Vvec(in),'r.'); % points inside

plot(Uvec(~in),Vvec(~in),'b.') % points outside

% plot(squeeze(U(xind,yind,:)),squeeze(V(xind,yind,:)),'k.')

axis equal

xlabel('U, m/s')

ylabel('V, m/s')

%% Scatter Plot CIRCLE

% figure

% Uvec = squeeze(U(xind,yind,:));Vvec = squeeze(V(xind,yind,:));

% U0 = nanmean(Uvec); V0 = nanmean(Vvec); % Circle center

% p = nsidedpoly(1000, 'Center', [U0 V0], 'Radius', 30);

% xv = p.Vertices(:,1); yv = p.Vertices(:,2);

% plot(xv,yv); hold on;

% [in,on] = inpolygon(Uvec,Vvec,xv,yv);

% plot(Uvec(in),Vvec(in),'r.'); % points inside

% plot(Uvec(~in),Vvec(~in),'b.') % points outside

% % plot(squeeze(U(xind,yind,:)),squeeze(V(xind,yind,:)),'k.')

% axis equal

% xlabel('U, m/s')

% ylabel('V, m/s')

ANKUR KUMAR
on 11 Jul 2021

Edited: ANKUR KUMAR
on 11 Jul 2021

There are multiple methods to select the center of the oval where density is maximum. One of the methods is just by counting the points at each pixel (of any threshold), and subsequently get the approx center of the oval. Once you get the optimum center of the oval (or circle), you can iterate the size of oval (or circle) to get the optimum length of the oval which covers the most dense area.

Just for the simplicity, I have taken circle instaead of oval. But I think this would be a good starting point for you to develop your thought process so that you can extend this code to oval.

clc;clear;close all;

load(['clean_data.mat'])

t = linspace(0,2*pi,5000);

theta = deg2rad(90); % vertical ellipse

threshold=10;

U_vec_array=[min(Uvec):threshold:max(Uvec)];

V_vec_array=[min(Vvec):threshold:max(Vvec)];

size_dist= arrayfun(@(u) arrayfun(@(v) size(find(Uvec>u & Uvec<=u+threshold & Vvec>v & Vvec<=v+threshold),1),...

V_vec_array), U_vec_array, 'uni', 0);

size_dist_matrix=cat(1,size_dist{:});

[uvec_max,vvec_max]=find(size_dist_matrix==max(size_dist_matrix(:)))

U0=U_vec_array(uvec_max);

V0=V_vec_array(vvec_max);

% Iterating over diff radius

for radius = 5:5:20

a = radius; b = radius; % major and minor radii

xv = U0 + a*cos(t)*cos(theta) - b*sin(t)*sin(theta);

yv = V0 + b*sin(t)*cos(theta) + a*cos(t)*sin(theta);

[in,on] = inpolygon(Uvec,Vvec,xv,yv); % Points inside and on oval

sprintf('Number of points lies in the circle of center (%d, %d), and radius %d is %d:', U0, V0, radius, size(Uvec(in),1))

end

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

Start Hunting!