move points on the edges of 2D grid, code fails for big data.

2 views (last 30 days)
Here is my code which creates 2D grid from min and max range.
And it moves data points from top edge and right edge to the center of closest bin.
This code fails to give desired output for big (25000 points) data.
x = [2,8,4,4.2,7.3,7.5,7.1,2,4.4,3.2,3.2,2,3.2,6,6.8,6.8,8]'; %Input vectors
y = [2,8,6,6.7,2.1,2.9,2.5,8,6.8,6.9,3.2,3.2,6.8,8,2,2,5]';
xy = [x,y];
r = x; % store original vector to r and s variable
s = y;
rs = [r,s];
numele = size(xy,1);
N =6;
%Extreme values of both vectors, actual valued are commented
mn_x = min(x); % 0.4482
mx_x = max(x); % 0.6238
mn_y = min(y); % -3.5596e+04
mx_y = max(y); % 9.1373e+04
dx = (mx_x - mn_x) / N; % width of bins
dy = (mx_y - mn_y) / N;
xc = [mn_x+(dx/2):dx:mx_x-(dx/2)]; %centers of bins
yc = [mn_y+(dy/2):dy:mx_y-(dy/2)];
x_rng = linspace(mn_x,mx_x,N+1); %edges of bins
y_rng = linspace(mn_y,mx_y,N+1);
scatter(r,s,'b'); %plot original data with circles
set(gca,'XTick', x_rng);
set(gca,'YTick', y_rng);
grid on;
hold on;
% For above mentioned data it works perfec, for large data points don't move.
x(x ==y_rng(1,end)) = yc(1,end); % Move data points on top edge to closest bin center
y(y ==x_rng(1,end)) = xc(1,end); % Move data points on right edge to closest bin center
[p,q] = meshgrid(x_rng,y_rng);
scatter(x,y,'r','filled') % Plot new data with filled circles

Answers (0)

Community Treasure Hunt

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

Start Hunting!