Meshgrid with variable spacing
이전 댓글 표시
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
Rik
2020년 6월 10일
What do you mean? What is your intended output? What result are you looking for? Posting slow code with nested loops is fine, then we have an idea what result we should work towards.
Lennart Hinz
2020년 6월 10일
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)
Lennart Hinz
2020년 6월 10일
편집: Lennart Hinz
2020년 6월 10일
답변 (1개)
Ameer Hamza
2020년 6월 10일
편집: Ameer Hamza
2020년 6월 10일
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
Lennart Hinz
2020년 6월 10일
Ameer Hamza
2020년 6월 10일
Can you elaborate "segment by segment constant density"? Can you show an example image?
Lennart Hinz
2020년 6월 10일
편집: Lennart Hinz
2020년 6월 10일
Ameer Hamza
2020년 6월 10일
편집: Ameer Hamza
2020년 6월 10일
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.
Lennart Hinz
2020년 6월 10일
편집: Lennart Hinz
2020년 6월 10일
카테고리
도움말 센터 및 File Exchange에서 Color Segmentation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!