How to traverse a range of three-dimensional space to calculate the function and draw the function diagram

1 次查看(过去 30 天)
My question is maily about traverse 3d space and draw the function result.
In the process of looking up information, I noticed that many people use the meshgrid function, but their functions can be written in simple expressions,just as follows:
x=linspace(0:1:10);
y=linspace(0:1:10);
z=linspace(0:1:10);
[X,Y,Z] = meshgrid(x,y,z)
F = X.*Y.*Z;
What if F cannot be written as a simple expression? Can only be written as F = func (x, y, z).
I've tried the following form, but it doesn't work.
F=func(X.,Y.,Z.)
So I want to ask, is there any other way besides three-tier nested loops?Because the calculation efficiency of this method is relatively low.Just as follows:
for i = x
for j=y
for k =z
F = func(i,j,k);
end
end
end
In addition, another problem is how to express the value of F in three-dimensional space (distinguished by color) after calculating F = func (x, y, z).
Selecting part of the plane through slice function is one of the methods. Is there any other way?
I look forward to your reply. Any help will be greatly appreciated!
  1 个评论
俊鹏 陈
俊鹏 陈 2022-3-29
To supplement, I find that my calculation result res is an n * 4 matrix, and each column corresponds to the result of x, y, Z and the function f = func (x, y, z). How to express the results in three-dimensional space?

请先登录,再进行评论。

回答(1 个)

Bjorn Gustavsson
Bjorn Gustavsson 2022-3-29
It might be far better to write your function func as "vectorized" as possible and send the full 3-D arrays into it at once. Once inside that function you cn then iterate over suitable 2-D slices through the 3-D arrays in the best possible manner (exactly what "best possible manner" is will obviously depend on the exact function you're calculating, and the details of the algorithm you chose, for finding the bottle-necks you should consider using the profiling capabilities of matlab.) Something like this:
function F = func(X,Y,Z)
F = zeros(size(Z));
for i3 = size(Z,3):-1:1 % trick sometimes useful to avoid re-allocation-slowdowns when it is
% cumbersome to figure out the size of the output
F(:,:,i3) = Z(:,:,i1).*sin(X(:,:,i3).*cos(Y(:,:,i3)));
end
end
Then you have pushed the looping/vectorized calculation as close to the actual calculations as possible, this is typically preferable. Also see the help and documentation for arrayfun and cellfun, they are typically handy for complicated functions that are not easily vectorizable.
For the 3-D displaying, you can get some nice volume rendering by using the alpha-transparency capability of matlab to plot multiple semi-transparent slices with slice. Another option (that I've never liked) is to use isosurface. If you have few points you might get something useful with scatter3. Also look at the file exchange for volume rendering, or volume visualization and what other combinations of terms you might come up with, FEX is a tresure-cave full with useful functions.
HTH
  3 个评论
Bjorn Gustavsson
Bjorn Gustavsson 2022-3-30
If you only have 20-by-20-by-20 you can try silce with some sensible choise of where to put the slices. You can also simply use subplots for each slice, and plot each slice in a separate subplot. If you have 20^3 data-points you can also use scatter3 without much hassle. Without knowing just anyting except that you have some number of regularly spaced points in 3-D at which you have an arbitrary scalar function it is impossible to give more than the most general advice. Your best line of action is to start trying different ways to plot the data, you know what you want to show with your figure, and the best way to do that is trying all sort of variations of plots to see if something works better.
俊鹏 陈
俊鹏 陈 2022-3-30
Yes, I think you are right. In my data, n is about the order of 10 ^ 6 ~ 8. I think I should try to draw slices separately.
Finally, thank you for your answer. I wish you a happy work and life!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by