Get bin label associated with 2D histogram of x,y data

Hello,
I am looking for a way to extract the bin labels from a large matrix of x and y coordinates. So the basic form of it would look like this:
A(:,1) = randi([50 550],500,1); % random x coord
A(:,2) = randi([50 250],500,1); % random y coord
Hist = hist3(A, [25 25]);
Is it possible to get the bin label associated with each of my 500 datapoints? Bigger picture is that all these coordinates in fact have some value and I want to average all these values for the bin they were found in based on their coordinates, before processing them further. Thanks for any help/advice!
J
edit: no I don't want the count of the number of coords in each bin, I need the bin label (say i,j and running from 1,1 to 25,25) of each pair of coordinates x,y. Hope this makes sense... thanks!

1 Comment

By Bin label, do you mean the count of the number of data points in each bin? Please edit your originial question to clarify.

Sign in to comment.

Answers (1)

Yea, you'll be able to do this using histc() and accumarray():
x = ceil(rand(1000,1)*10); %x
y = ceil(rand(1000,1)*10); %y
z = rand(1000,1); %values
edges = 1:10; %some edges to bin along
[~, idxx] = histc(x,edges); %Which x bin?
[~, idxy] = histc(y,edges); %Which y bin?
averages = accumarray([idxx idxy],z,[],@mean); %Build the average matrix

Categories

Asked:

on 21 Feb 2013

Community Treasure Hunt

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

Start Hunting!