我的网格为512 * 1024 * 488,(x,y,z)我有以下脚本来读取数据,然后绘制2d切片的轮廓
%function [rtime,itime,dt,nx,ny,nz,deltax,deltay,rlenz,u,v,w] = Read_vel3d(name)
% _________________________________________________________________________
% name;
fid = fopen('vel.451000', 'rb');
% fid;
rtime = fread(fid, 1, 'float64');
itime = fread(fid, 1, 'int32');
dt = fread(fid, 1, 'float64');
nx0 = fread(fid, 1, 'int32');
ny0 = fread(fid, 1, 'int32');
nz = fread(fid, 1, 'int32');
deltax = fread(fid, 1, 'float64');
deltay = fread(fid, 1, 'float64');
rlenz = fread(fid, 1, 'float64');
utoto = fread(fid, nx0*ny0*nz, 'float64');
vtoto = fread(fid, nx0*ny0*nz, 'float64');
wtoto = fread(fid, nx0*ny0*nz, 'float64');
fclose(fid);
u = reshape(utoto,[nx0,ny0,nz]);
v = reshape(vtoto,[nx0,ny0,nz]);
w = reshape(wtoto,[nx0,ny0,nz]);
nx = nx0-1;
ny = ny0-1;
display('Contour Plot');
A = squeeze(u(138,:,:));
figure
figure('visible','on')
contourf(A);
colormap jet;
hold on
colorbar;
我明白我什么时候做的:
A = squeeze(u(:,:,138));
它在z中创建一个单独的维度,然后将数组绘制为1024 * 512,在x上为1024,然后在y中为512,这是正确的,但是当我这样做时:
A = squeeze(u(138,:,:));
它再次在x中创建一个单独的维度,然后绘制数组488 * 1024,在x中为488,然后在y中为1024,这就是问题所在。在这个切片中x应该是1024而y应该是488,但我不确定如何做到这一点。
答案 0 :(得分:0)
从高维数据中获取2D切片的一种方法是使用 squeeze ,并在需要时(取决于您的方向偏好)将切片转换为建议的注释之一。但是,我建议使用 permute 。 置换的优点在于,在极少数情况下,切片的维度是单数,您仍然可以获得正确的方向,而 squeeze 会挤出单个维度。< / p>
无论哪种方式,当您从不同的维度中提取时,您将需要采用不同的方式:
if isDim1 % extract slice from 1st dim
A = permute(u(138, :, :), [2 3 1]); % could be [3 2 1]
elseif isDim2 % extract slice from 2nd dim
A = permute(u(:, 138, :), [3 1 2]); % could be [1 3 2]
else % 3rd dim
A = u(:, :, 138); % could be A = permute(u(:, :, 138), [2 1 3]);
end