Not sure whether this solves your question completely, but if your problem is defining the 'buckets' dynamically you can do this:
% Find the minimum and maximum of the matrix
Mmin = min(M(:));
Mmax = max(M(:));
% Assume you have a matrix M with positive and negative values, and want it in bins of 0.2
buckets = Mmin:0.2:Mmax;
% OR assume you want to spread them equally over a fixed amount of bins, say 100
buckets = linspace(Mmin,100,Mmax);
EDIT:
Suppose you want to devide the matrix based on the values of one column, say 3, then you can do it like this:
% Define the relevant column as a vector for easy handling
v = M(:,3);
% Assume you want to spread them equally over a fixed amount of bins, say 100
buckets = linspace(min(v),100,max(v));
% Now see which column belongs in each bucket
bucket_idx = ones(size(v));
for i = 2:length(buckets)
bucket_idx(v>buckets(i-1)&(v<buckets(i)) = i;
end
This tells you in which bucket each row belongs, it would be nicer to vectorize this but at the moment this is the quickest solution I can think of. I think you should be able to solve the rest of the problem once you know in which bucket everything belongs.