排序二维坐标转换成在MATLAB箱 [英] Sorting 2D coordinates into bins in MATLAB

查看:340
本文介绍了排序二维坐标转换成在MATLAB箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用MATLAB到由电网定义的垃圾箱二维笛卡尔网格随机坐标进行排序。

I am trying to sort random coordinates on a 2D cartesian grid using MATLAB into "bins" defined by a grid.

例如,如果我有一个二维域以X,从[-1,1]和Y从[-1,1],我产生域内一些随机的坐标,我怎么能算有多少坐标下降到每一个象限?

For example if I have a 2D domain with X ranging from [-1,1] and Y from [-1,1] and I generate some random coordinates within the domain, how can I "count" how many coordinates fall into each quadrant?

我意识到,如果语句可以用来确定是否每个坐标象限内,但我想这扩展到更大的方形网格有不止4个象限。

I realize that for and if statements can be used to determine the if each coordinate is within the quadrants, but I would like to scale this to much larger square grids that have more than just 4 quadrants.

任何简洁高效的办法是AP preciated!

Any concise and efficient approach would be appreciated!

推荐答案

下面是改编自的code我提到的一个例子

由此产生的离散化点存储变量潜艇;每一行都包含到一个点被分配垃圾桶的2D标指标。

The resulting binned points are be stored the variable subs; Each row contains 2d subscript indices of the bin to which a point was assigned.

% 2D points, both coordinates in the range [-1,1]
XY = rand(1000,2)*2 - 1;

% define equal-sized bins that divide the [-1,1] grid into 10x10 quadrants
mn = [-1 -1]; mx = [1 1];  % mn = min(XY); mx = max(XY);
N = 10;
edges = linspace(mn(1), mx(1), N+1);

% map points to bins
% We fix HISTC handling of last edge, so the intervals become:
% [-1, -0.8), [-0.8, -0.6), ..., [0.6, 0.8), [0.8, 1]
% (note the last interval is closed on the right side)
[~,subs] = histc(XY, edges, 1); 
subs(subs==N+1) = N;

% 2D histogram of bins count
H = accumarray(subs, 1, [N N]);

% plot histogram
imagesc(H.'); axis image xy
set(gca, 'TickDir','out')
colormap gray; colorbar
xlabel('X'); ylabel('Y')

% show bin intervals
ticks = (0:N)+0.5;
labels = strtrim(cellstr(num2str(edges(:),'%g')));
set(gca, 'XTick',ticks, 'XTickLabel',labels, ...
    'YTick',ticks, 'YTickLabel',labels)

% plot 2D points on top, correctly scaled from [-1,1] to [0,N]+0.5
XY2 = bsxfun(@rdivide, bsxfun(@minus, XY, mn), mx-mn) * N + 0.5;
line(XY2(:,1), XY2(:,2), 'LineStyle','none', 'Marker','.', 'Color','r')

这篇关于排序二维坐标转换成在MATLAB箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆