Allocate data into grid boxes

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

Guillaume
Guillaume on 22 Nov 2016
Edited: Guillaume on 22 Nov 2016
Well, I don't really see the difficulty. Simply use histcounts2 or histogram2 (for the plot) with the 'Normalization', 'probability' option and you're done:
X = num2cell(randi(20, 1, 316)); %demo data. Why is it in a cell array?
Y = num2cell(randi(10, 1, 316)); %demo data. Why is it in a cell array?
NoOfBins = 20; %or whatever you want
X = cell2mat(X); Y = cell2mat(Y); %There is no point having X and Y in cell array. Convert to matrix for easier use
[gridprob, xedges, yedges] = histcounts2(X, Y, NoOfBins, 'Normalization', 'probability');
surf(mean(xedges([1:end-1;2:end])), mean(yedges([1:end-1;2:end])), gridprob)
%or use
%histogram2(X, Y, NoOfBins, 'Normalization', 'probability')
Done!
As per the comments, there is absolutely no point in having cell arrays of scalar. use a standard vector instead.

3 Comments

Hi Guillaume, Thanks for your help.Yes, i already converted X and Y into vectors instead of cell arrays.
Here, histogram2 or histcounts2 both are not supported in my matlab i guess since its saying undefined functions or variables.I am using Matlab version R2015a.
Well, time to upgrade! At least to R2015b where the functions were introduced.
Failing that, you can do the binning with histcounts on each dimension and a final accumarray but you have to do the normalisation yourself. This would probably work:
[~, xedges, binx] = histcounts(X, NoOfBins); %binning along X
[~, yedges, biny] = histcounts(Y, NoOfBins); %binning along Y
gridprob = accumarray([binx(:), biny(:)], 1, [numel(xedges), numel(yedges)] - 1); %accumulate in 2D
gridprob = gridprob / sum(gridprob(:)); %normalise
Jung BC
Jung BC on 23 Nov 2016
Edited: Jung BC on 24 Nov 2016
Hi,
I upgraded my matlab to R2016b, now the first solution is already working well.Thank you for your support!

Sign in to comment.

More Answers (1)

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

Hi,
Thanks for your solution. But it won't define my overal problem.
Homero Noboa
Homero Noboa on 10 Feb 2021
Edited: Homero Noboa on 10 Feb 2021
Would you have the code to allocate data into 3D grids?

Sign in to comment.

Categories

Products

Asked:

on 22 Nov 2016

Edited:

on 10 Feb 2021

Community Treasure Hunt

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

Start Hunting!