Azzera filtri
Azzera filtri

Allocate data into grid boxes

13 visualizzazioni (ultimi 30 giorni)
Jung BC
Jung BC il 22 Nov 2016
Modificato: Homero Noboa il 10 Feb 2021
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!!!

Risposta accettata

Guillaume
Guillaume il 22 Nov 2016
Modificato: Guillaume il 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 Commenti
Guillaume
Guillaume il 22 Nov 2016
Modificato: Guillaume il 22 Nov 2016
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 il 23 Nov 2016
Modificato: Jung BC il 24 Nov 2016
Hi,
I upgraded my matlab to R2016b, now the first solution is already working well.Thank you for your support!

Accedi per commentare.

Più risposte (1)

KSSV
KSSV il 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 Commenti
Jung BC
Jung BC il 22 Nov 2016
Hi,
Thanks for your solution. But it won't define my overal problem.
Homero Noboa
Homero Noboa il 10 Feb 2021
Modificato: Homero Noboa il 10 Feb 2021
Would you have the code to allocate data into 3D grids?

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by