-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgetDistMatrix.m
60 lines (46 loc) · 1.43 KB
/
getDistMatrix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function distxy = getDistMatrix( siz, refpos )
%GETDISTMATRIX Calculate the distances in a N-D grid from one
% reference point to all other points on the grid.
if nargin==0
% demo mode
siz = [4 3];
refpos = [1 1];
end
siz = siz(:)';
refpos = refpos(:)';
N = numel(siz);
M = prod(siz);
if numel(refpos)~=N
error('argument mismatch');
end
if any(refpos>siz)||any(refpos<1)
error('reference position out of matrix scope');
end
% % speedup: take advantage of mirror symmetry around refpos
% maxsubsize = max([siz-refpos+1 ; refpos],[],1);
%
% % speedup: take advantage of point symmetry around refpos
% % ...
% just calculate it all
distxy = zeros(siz);
for ii=1:numel(siz)
s = siz; s(ii) = [];
x = ((1:siz(ii))' - refpos(ii)).^2;
% speedup: instead of repmat, use tmpdist(ones(...),...) or something alike
% distxy = distxy + reshape( x(:,ones(prod(s),1)),siz);
distxy = distxy + permute(reshape( x(:,ones(prod(s),1)),[siz(ii) s]),[2:ii 1 ii+1:N]);
end
distxy = sqrt(distxy);
% % % % % just calculate it all
% % % % distxy = zeros(sizeM);
% % % % for ii=1:numel(sizeM)
% % % % tmpdist = ((1:sizeM(ii)) - refpos(ii)).^2;
% % % % tmpsize = [ones(1,ii-1) sizeM(ii) ones(1,N-ii)];
% % % % tmpsize2 = sizeM - tmpsize+1;
% % % %
% % % % distxy = distxy + repmat(reshape(tmpdist,tmpsize),tmpsize2);
% % % % % speedup: instead of repmat, use tmpdist(ones(...),...) or something alike
% % % % end
% % % %
% % % % distxy = sqrt(distxy);
end