我想创建由两个向量定义的大小的零点,这将在 2D 和 3D 中工作。
%% Setting in 2D
% rectangular domain with left bottom corner [_1,_2]
% and right top corner [_1,_2]
lb_corner=[-1,-2]
rt_corner=[2,2]
domain_size=rt_corner - lb_corner;
dim=size(domain_size,2);
nb_pixels=[5,4]; % number of pixels in [x_1,x_2]
%% Mesh parameters
pixel_size= domain_size./nb_pixels;
%% Coordinates
if dim == 2
x_coords=zeros(nb_pixels(1),nb_pixels(2),dim);
elseif dim == 3
x_coords=zeros(nb_pixels(1),nb_pixels(2),nb_pixels(3),dim);
end
我想用类似的东西替换 if 条件:
% x_coords=zeros(nb_pixels,dim);
这应该在二维中创建大小为
(nb_pixels(1),nb_pixels(2),dim)
的数组,
和 3D中大小为
(nb_pixels(1),nb_pixels(2),nb_pixels(3),dim)
的数组
将
nb_pixels
更改为元胞数组并使用 {:}
将其元素作为逗号分隔列表获取:
dim = 2;
nb_pixels = {5, 4};
x_coords=zeros(nb_pixels{:}, dim)
x_coords =
ans(:,:,1) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
ans(:,:,2) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
dim = 2;
nb_pixels = {5, 4, 2};
x_coords=zeros(nb_pixels{:}, dim)
x_coords =
ans(:,:,1,1) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
ans(:,:,2,1) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
ans(:,:,1,2) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
ans(:,:,2,2) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0