use*_*863 7 matlab projection matrix vectorization
我有一个3D点云(XYZ),Z
可以是位置或能量.我希望将它们投影在n- by- m网格的2D表面上(在我的问题中n = m
),其方式是每个网格单元具有最大差值的值Z
,如果Z
是位置,或者总和的值.Z
在Z
能量的情况下.
例如,在一定范围内0 <= (x,y) <= 20
,有500个点.假设xy平面具有n - by - m分区,例如4- by- 4 ; 我的意思是在两个方向x
和y
方向上我们有4个分区,间隔为5
(使其20
最大.现在,这些单元格中的每一个都应该具有这些单元中的值的总和或最大差异的Z
值.定义的xy平面中的相应列.
我制作了一个简单的XYZ阵列,仅用于测试,如下所示,在这种情况下,Z
表示每个点的能量.
n=1;
for i=1:2*round(random('Uniform',1,5))
for j=1:2*round(random('Uniform',1,5))
table(n,:)=[i,j,random('normal',1,1)];
n=n+1;
end
end
Run Code Online (Sandbox Code Playgroud)
如何在没有循环的情况下完成?
该accumarray
功能非常适合这种任务.首先我定义示例数据:
table = [ 20*rand(1000,1) 30*rand(1000,1) 40*rand(1000,1)]; % random data
x_partition = 0:2:20; % partition of x axis
y_partition = 0:5:30; % partition of y axis
Run Code Online (Sandbox Code Playgroud)
我在假设
table
代表x,y,zNaN
(如果您想要一些其他填充值,只需更改最后一个参数accumarray
).然后:
L = size(table,1);
M = length(x_partition);
N = length(y_partition);
[~, ii] = max(repmat(table(:,1),1,M) <= repmat(x_partition,L,1),[],2);
[~, jj] = max(repmat(table(:,2),1,N) <= repmat(y_partition,L,1),[],2);
ii = ii-1; % by assumption, all values in ii will be at least 2, so we subtract 1
jj = jj-1; % same for jj
result_maxdif = accumarray([ii jj], table(:,3), [M-1 N-1], @(v) max(v)-min(v), NaN);
result_sum = accumarray([ii jj], table(:,3), [M-1 N-1], @sum, NaN);
Run Code Online (Sandbox Code Playgroud)
代码注释:
ii
和jj
,它给出了每个点所在的x和y区的索引.我习惯repmat
这样做.它本来更好用bsxfun
,但它不支持多输出版本@max
.评论:
你能做的是
meshgrid
,kd-tree
,即将与每个云点关联的数据标记为网格节点accumarray
)。这是一个工作示例:
samples = 500;
%data extrema
xl = 0; xr = 1; yl = 0; yr = 1;
% # grid points
sz = 20;
% # new random cloud
table = [random('Uniform',xl,xr,[samples,1]) , random('Uniform',yr,yl,[samples,1]), random('normal',1,1,[samples,1])];
figure; scatter3(table(:,1),table(:,2),table(:,3));
% # grid construction
xx = linspace(xl,xr,sz); yy = linspace(yl,yr,sz);
[X,Y] = meshgrid(xx,yy);
grid_centers = [X(:),Y(:)];
x = table(:,1); y = table(:,2);
% # kd-tree
kdtreeobj = KDTreeSearcher(grid_centers);
clss = kdtreeobj.knnsearch([x,y]); % # classification
% # defintion of local statistic
local_stat = @(x)sum(x) % # for total energy
% local_stat = @(x)max(x)-min(x) % # for position off-set
% # data_grouping
class_stat = accumarray(clss,table(:,3),[],local_stat );
class_stat_M = reshape(class_stat , size(X)); % # 2D reshaping
figure; contourf(xx,yy,class_stat_M,20);
Run Code Online (Sandbox Code Playgroud)