2

I have written a function in Matlab that gives me a vector at a position (x,y,z).

Now I am looking for the easiest way to make a colored map of this field on a grid and the color should be related to the norm of the vector.

What is the easiest way to define such a grid for $x \in [x_0,x_1],y \in [y_0,y_1], z \in [z_0,z_1]$? Probably linspace for each component would be possible, but maybe there is already a command that gives me the grid.

Now I need to evaluate my function. The problem is, that it actually gives me two vectors, but I am only interested in the first one. So when I first tried to do this I thought that $[A(i,j,k),~]=function(x(i),y(j),z(k))$ could work, but it did not(My goal was: Choose the first vector(A) and mark him with the reference(i,j,k), so that you later on know to which coordinates this vector belongs to).

So I am highly interested in any kind of ideas.

4

2 回答 2

2

该函数meshgrid可能是您正在寻找生成 x、y 和 z 坐标的函数。

于 2013-08-07T16:48:45.030 回答
0

代替

[A(i,j,k),~]=function(x(i),y(j),z(k));

尝试

[A(i,j,k,:),~]=function(x(i),y(j),z(k));

这样您就可以适应 3 坐标向量的整个大小。此外,如果您想预先分配空间使用

 A = zeros(Nx,Ny,Nz,3);

Nx,...你的坐标空间的暗淡在哪里。然后就像@Moly 解释的那样,meshgrid用来生成一个 3D 网格,

 [X Y Z] = meshgrid(x,y,z);

并循环或矢量化以解析函数在点处的值X(i,j,k),Y(i,j,k),Z(i,j,k),计算norm并将其存储在 3D 数组C中。

编辑

用 表示立方体mesh(X,Y,Z,C)是不可能的,但可以用 表示 3D 立方体的各个切片mesh,将高度设置为Z等于函数的结果C。然后需要一些额外的工作才能正确着色。

另一种选择是使用pcoloror contourf。除了创建 3D 等值面之外,这可能是显示 4D 数据的最简单方法。

代码:

figure
colormap(jet)
for ii=1:9
    subplot(3,3,ii)
    pcolor(X(:,:,ii),Y(:,:,ii),F(:,:,ii))
    axis('equal','square'), title(['Z=' num2str(ii)])
    caxis([0 1])
end

在此处输入图像描述

于 2013-08-07T18:07:23.350 回答