我将数据x
,y
,z
和v
组织为列向量。可以通过以下代码获得类似的数据:
N = 5;
[xf,yf,zf,vf] = flow(N);
ux = unique(xf);
uy = unique(yf);
uz = unique(zf);
x = nan(numel(xf),1);
y = nan(numel(yf),1);
z = nan(numel(zf),1);
v = nan(numel(vf),1);
iCount = 1;
for iX = 1:numel(ux)
for iY = 1:numel(uy)
for iZ = 1:numel(uz)
x(iCount) = ux(iX);
y(iCount) = uy(iY);
z(iCount) = uz(iZ);
v(iCount) = vf((xf == x(iCount))&(yf == y(iCount))&(zf == z(iCount)));
iCount = iCount+1;
end
end
end
我无法更改数据生成的方式,因此我需要对其重塑形状,以便稍后与isosurface()
和griddedInterpolant()
一起使用。原始数据的大小相当大,我想避免循环。
reshape()
函数的简单用法:
X = reshape(x,[N,2*N,N]);
Y = reshape(y,[N,2*N,N]);
Z = reshape(z,[N,2*N,N]);
V = reshape(v,[N,2*N,N]);
isosurface(X,Y,Z,V,-3)
告诉我Input grid is not a valid MESHGRID.
您能帮助我以适当的方式重塑数据吗?
答案 0 :(得分:1)
数组中的项目顺序与meshgrid
中的列主要顺序不匹配。差不多完成了,但是您需要用不同的形状重塑1d数组的形状,然后将结果置换回所需的实际形状:
N = 5;
[xf,yf,zf,vf] = flow(N);
ux = unique(xf);
uy = unique(yf);
uz = unique(zf);
x = nan(numel(xf),1);
y = nan(numel(yf),1);
z = nan(numel(zf),1);
v = nan(numel(vf),1);
iCount = 1;
for iX = 1:numel(ux)
for iY = 1:numel(uy)
for iZ = 1:numel(uz)
x(iCount) = ux(iX);
y(iCount) = uy(iY);
z(iCount) = uz(iZ);
v(iCount) = vf((xf == x(iCount))&(yf == y(iCount))&(zf == z(iCount)));
iCount = iCount+1;
end
end
end
% new stuff starts here
X = permute(reshape(x,[N,N,2*N]),[2,3,1]);
Y = permute(reshape(y,[N,N,2*N]),[2,3,1]);
Z = permute(reshape(z,[N,N,2*N]),[2,3,1]);
V = permute(reshape(v,[N,N,2*N]),[2,3,1]);
% check equivalence
isequal(X,xf)
isequal(Y,yf)
isequal(Z,zf)
isequal(V,vf)
以上表明输入数组已被复制(我们得到四个逻辑1
)。
请注意,您的测试用例应该更加不对称,因为现在某些输入数组是彼此的转置,并且三个维度中的两个具有相同的大小。如果您的测试使用非对称大小(即[N,M,K]
),那么找出可能的歧义会容易得多,因为在这种情况下所有维度都是不等价的:
N = 2; M = 3; K = 4;
[xf,yf,zf] = meshgrid(1:N,1:M,1:K);
vf = flow(xf,yf,zf);
ux = unique(xf);
uy = unique(yf);
uz = unique(zf);
x = nan(numel(xf),1);
y = nan(numel(yf),1);
z = nan(numel(zf),1);
v = nan(numel(vf),1);
iCount = 1;
for iX = 1:numel(ux)
for iY = 1:numel(uy)
for iZ = 1:numel(uz)
x(iCount) = ux(iX);
y(iCount) = uy(iY);
z(iCount) = uz(iZ);
v(iCount) = vf((xf == x(iCount))&(yf == y(iCount))&(zf == z(iCount)));
iCount = iCount+1;
end
end
end
X = permute(reshape(x,[K,M,N]),[2,3,1]);
Y = permute(reshape(y,[K,M,N]),[2,3,1]);
Z = permute(reshape(z,[K,M,N]),[2,3,1]);
V = permute(reshape(v,[K,M,N]),[2,3,1]);
% check equivalence
isequal(X,xf)
isequal(Y,yf)
isequal(Z,zf)
isequal(V,vf)