简体   繁体   中英

Creating a sequence of grid points in MATLAB

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM