-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeGrid.m
More file actions
53 lines (48 loc) · 1.51 KB
/
MakeGrid.m
File metadata and controls
53 lines (48 loc) · 1.51 KB
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
function [coOrds,X,Y,Z] = MakeGrid(extent,resolution,numDims,subsampleSpace)
% Generate an (X,Y,[Z]) grid for embedding spatial maps onto
% --extent is the linear spatial extent in each dimension
% --resolution is the total number of points to split along each dimension (unitless)
%-------------------------------------------------------------------------------
if nargin < 1
extent = 10;
end
if nargin < 2
resolution = 25;
end
if nargin < 3
numDims = 2;
end
assert(ismember(numDims,[2,3]));
if length(extent)==1
% Isotropic by defualt
extent = ones(numDims,1)*extent;
end
assert(numDims==length(extent))
if nargin < 4
subsampleSpace = []; % keep all samples
end
%-------------------------------------------------------------------------------
% Set up isotropic numDims-dimensional grid:
xRange = linspace(0,extent(1),resolution);
yRange = linspace(0,extent(2),resolution);
if numDims==3
zRange = linspace(0,extent(3),resolution);
end
%-------------------------------------------------------------------------------
if numDims==2
[X,Y] = meshgrid(xRange,yRange);
coOrds = [X(:),Y(:)];
Z = [];
else
[X,Y,Z] = meshgrid(xRange,yRange,zRange);
coOrds = [X(:),Y(:),Z(:)];
end
%-------------------------------------------------------------------------------
% Sub-sample
if ~isempty(subsampleSpace)
rp = randperm(length(coOrds));
keepMe = rp(1:subsampleSpace);
coOrds = coOrds(keepMe,:);
fprintf(1,'Subsampling %u down to %u spatial points\n',length(rp),length(coOrds));
end
end