Reputation: 203
The dimension n
is given, we want to grid the space with equidistant points, let say in each dimension we start from -L
to +L
by a step size 2L/(N+1)
. Now, we would like the output to be all the n-dimensional vectors giving the nodal points. They are all contained in an array of n
rows and N^n
columns. I am wondering how to code this in MATLAB.
Thanks in advance,
Upvotes: 1
Views: 269
Reputation: 10440
If I understand you correctly, this is what you look for:
n = 3; % no. of dimensions
L = 4; % bounds
N = 20; % no. of points
grd = cell(1,n);
[grd{:}] = ndgrid(-L:2*L/(N-1):L);
here grd
is a cell array of with n
cells, each for one dimension. Note that if you want N
points you need to set the gap to 2*L/(N-1)
because 2*L/N
will give you N+1
points.
grd =
[20x20x20 double] [20x20x20 double] [20x20x20 double]
If you want the output to be in an n
by N^n
matrix, you loop through grd
:
arr = zeros(n,N^n);
for k = 1:n
arr(k,:) = grd{k}(:).';
end
and you get:
>> size(arr)
ans =
3 8000
Upvotes: 2