How can one create this matrix, part 2

1 回表示 (過去 30 日間)
alpedhuez
alpedhuez 2020 年 7 月 24 日
編集済み: Bruno Luong 2020 年 7 月 25 日
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 日
[X, Y, Z] = ndgrid(0:.1:0.5);
[Z(:), Y(:), X(:)]
  5 件のコメント
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 件)

カテゴリ

Help Center および File ExchangeConstruction についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by