In the previous post, I have learned that
[X, Y] = meshgrid(0:.1:0.5);
[X(:), Y(:)]
will create a matrix
0 0
0 0.1
0 0.2
...
Now I want to generalize to
[X, Y, Z] = meshgrid(0:.1:0.5);
[X(:), Y(:), Z(:)]
and it creates a matrix
ans = 216×3
0 0 0
0 0.1000 0
0 0.2000 0
0 0.3000 0
0 0.4000 0
0 0.5000 0
0.1000 0 0
0.1000 0.1000 0
0.1000 0.2000 0
0.1000 0.3000 0
But the matrix I want to create is like
0 0 0
0 0 0.1
0 0 0.2
0 0 0.3
0 0 0.4
0 0 0.5
0 0.1 0.1
0 0.1 0.2
0 0.1 0.3
Please advise.

 채택된 답변

madhan ravi
madhan ravi 2020년 7월 24일

0 개 추천

[X, Y, Z] = ndgrid(0:.1:0.5);
[Z(:), Y(:), X(:)]

댓글 수: 5

alpedhuez
alpedhuez 2020년 7월 24일
Thank you. But how does it work?
Bruno Luong
Bruno Luong 2020년 7월 24일
편집: Bruno Luong 2020년 7월 24일
Thank you. But how does it work?
Just think those combinations you look for
(1,1)
(1,2)
...
(1,5)
(2,1)
...
(5,5)
as 2D coordinates of a set of grid points in a rectangles (1:5) x (1:5).
For 3 combinations, it's a 3D coordinates of grid points of a cube (1:5) x (1:5) x (1:5),
ndgrid() function generates grid points in n-dimensional space.
alpedhuez
alpedhuez 2020년 7월 25일
Yes. But whey (Z,Y,X) not (X,Y,Z)?
Bruno Luong
Bruno Luong 2020년 7월 25일
편집: Bruno Luong 2020년 7월 25일
Because NDGRID returns result such that the first input change first (more rapidly) and you want the opposite, X change most slowly. Actually the code as written by madhan is confusing, it's clearer this way to me:
[Z,Y,X] = ndgrid(0:.1:0.5);
[X(:), Y(:), Z(:)]
You'll see if 1D-grid of X, Y, Z are different and not all equal to 0.0.1:0:5, it makes a whole world less confusing.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2020년 7월 24일

편집:

2020년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by