Meshgrid with variable spacing
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear community,
I need a three-dimensional point grid with variable spacing. In the example below, I would like to have a coarser spacing at the edges and a finer spacing in the middle.
X_vec=unique([-7:1:-3 -3:.1:3 3:1:7]);
Y_vec=unique([-7:1:-3 -3:.1:3 3:1:7]);
Z_vec=unique([-7:1:-3 -3:.1:3 3:1:7]);
[X,Y,Z]=meshgrid(X_vec, Y_vec, Z_vec);
pcshow([X(:) Y(:) Z(:)]);
As you can see, my approach does not really work, because the grid is also too fine in the edges. Does anyone have an idea how to modify this approach without calling the meshgrid function segment by segment?
Best regards
Lennart
4 commentaires
Rik
le 10 Juin 2020
Modifié(e) : Rik
le 10 Juin 2020
I'm not sure I understand the difference between what you want and what you currently have. Can you show the code with nested loops? If you have code that can't be adapted and is slow, we can compare the output of a faster, more flexible function to make sure the results are the same.
To give an example: if you wouldn't know about the sum function, you could describe it like this:
s=0;
for n=1:size(X,1)
for m=1:size(X,2)
s=s+X(n,m);
end
end
Which we could then test against faster code:
s2=sum(X,[1 2]); % or: s2=sum(X(:));
isequal(s2,s)
Réponses (1)
Ameer Hamza
le 10 Juin 2020
Modifié(e) : Ameer Hamza
le 10 Juin 2020
Try this or something similar to create the grid points
x = (-7:0.3:7);
x = sign(x).*x.^2;
X_vec=x;
Y_vec=x;
Z_vec=x;
[X,Y,Z]=meshgrid(X_vec, Y_vec, Z_vec);
pcshow([X(:) Y(:) Z(:)]);

Or something like this
R_ved = 0:0.05:1;
A_vec = 0:0.2:2*pi;
E_vec = 0:0.2:2*pi;
[R, A, E] = meshgrid(R_ved, A_vec, E_vec);
[X, Y, Z] = sph2cart(A, E, R);
pcshow([X(:) Y(:) Z(:)]);

If rectangular grid is requirement, then modify the above example
rect_limit = 1;
R_ved = 0:0.05:sqrt(3*rect_limit.^2);
A_vec = 0:0.2:2*pi;
E_vec = 0:0.2:2*pi;
[R, A, E] = meshgrid(R_ved, A_vec, E_vec);
[X, Y, Z] = sph2cart(A, E, R);
M = [X(:) Y(:) Z(:)];
idx = any(abs(M) > rect_limit, 2);
M(idx, :) = [];
pcshow(M);

5 commentaires
Ameer Hamza
le 10 Juin 2020
Modifié(e) : Ameer Hamza
le 10 Juin 2020
Ok! I understand the shape of the grid now. Do you only want to have 2 levels or multiple levels? What format of X_vec, Y_vec, and Z_vec you intend to use in the final code. For example, you can specify the levels like this
X_vec = [-7 -3 3 7];
% similar for Y_vec and Z_vec
or is there some other format?
Also, the visuals attached in the answers are images inserted manually after rendering. fig file cannot be directly visualized here.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!