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

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 Comment

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?

Sign in to comment.

Answers (1)

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 Comments

Thank you very much for your enthusiastic answer!Your answer has brought me great inspiration.
However, in the process of vectorizing the function, I found that I encountered more difficult things later (the variable dimension is too high in the solution process, which makes all kinds of functions difficult to use).
It also makes me want to give up this way.And now I don't want to tangle too much. I just want to express my n * 4 matrix (corresponding to x, y, z and f (x, y, z)) in three-dimensional space.
I'm sorry to ask you again. Do you have any good suggestions?
Take the following data as a reference.
n=0 %as a count
res = zeros(20*20*20,4);
for i=0:20:1
for j=0:20:1
for k = 0:20:1
f = (i+1)*j^2*k^3; %Don't care about the function here.
% I just want to generate an n * 4 matrix here
res(n,:) = [i,j,k,f];
n=n+1
end
end
end
%In fact, now I have such an n * 4 matrix res and need to plot it in a
%appropriate way.
I had planned to select several planes like slice and express them in color, but I didn't find a way to work.
Thank you very much for your previous answer, and I'm sorry to ask such a stupid question.
Eagerly looking forward to your reply!
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.
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!

Sign in to comment.

Asked:

on 29 Mar 2022

Commented:

on 30 Mar 2022

Community Treasure Hunt

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

Start Hunting!