Allocate data into grid boxes
Show older comments
Hello everyone,
I need your help.
I have two cell arrays of sizes 1x316 each and values inside them are double numeric in type. First cell array contains X co-ordinates and second is Y co-ordinates.
1) I want to plot all these x and y co-ordinates into an imaginary grid (square boxes) in 2D space to count the number of points that fall into each grid boxes determined by (X, Y) points.
The size of the grid squares isn’t fixed but it should include all these X and Y values ranging from X(min) to X(max) and Y(min) to Y(max),
Let’s say, for the grid structure:
Xgrid = [Xmin: Step: Xmax]
Ygrid = [Ymin: Step: Ymax]
Step = (Xmax - Xmin)/(No of Bins)
No of Bins = (Xmax – Xmin) / Step
2) While plotting the (X, Y) values into grid boxes, if they lie in the edges of boxes then there should be rounding mechanism which will force all points to be inside grid boxes.
3)I need to count probability of each points falling into grid boxes.
That means: if there is 5 points of (X, Y) inside any one grid box, let’s say box A, and total number of points in all boxes is 20, then probability of A should be 5:20 i.e., answer should return here 0.25.
Any suggestions, ideas, tricks? Thanks in advance!!!
Accepted Answer
More Answers (1)
KSSV
on 22 Nov 2016
Take use of this code:
clc; clear all ;
% make random data
data = rand(1000,2) ;
% divide into grid
M = 5 ; N = 5 ;
x = linspace(0,1,M) ;
y = linspace(0,1,N) ;
[X,Y] = meshgrid(x,y) ;
Z = zeros(size(X)) ;
figure
plot(data(:,1),data(:,2),'.r') ;
hold on
plot(X,Y,'k') ; plot(Y,X,'k')
%%Get points inside for each box
P = cell(M,N) ;
for i = 1:N-1
for j = 1:M-1
A = [X(i,j) Y(i,j)] ;
B = [X(i+1,j+1) Y(i+1,j+1)] ;
idx = find(data(:,1) >= A(1) & data(:,1) <B(1)) ;
idy = find(data(:,2) >= A(2) & data(:,2) <B(2)) ;
id = intersect(idx,idy) ;
P{i,j} = [data(id,1) data(id,2)] ;
% plot points inside first box
plot(P{i,j}(:,1),P{i,j}(:,2),'O','color',rand(1,3))
end
end
2 Comments
Jung BC
on 22 Nov 2016
Homero Noboa
on 10 Feb 2021
Edited: Homero Noboa
on 10 Feb 2021
Would you have the code to allocate data into 3D grids?
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!