Convert gridded data to coordinate matrix

Hello, I was wondering if there is some Matlab command to convert gridded data to a coordinate matrix. For instance, I have
X = -1:0.2:1;
Y = -1:0.2:1;
M = rand(11,11);
and M(i,j) represents the z-value of (X(i),Y(j)) (which is random in here, but usually this will be the value under some function F(x,y) of course). Now I would like to convert M to a matrix C of size 11*11 times 3, each row giving the coordinates of one grid point specified by M. I could run some for-loops and define the rows one by one, but I was wondering if there is some specific Matlab command for this.

 採用された回答

Julie
Julie 2018 年 8 月 21 日

1 投票

coord=repmat(1:11,11,1);
XYZmat=cat(3,X(coord),Y(coord'),M);

4 件のコメント

Julie
Julie 2018 年 8 月 21 日
Where XYZmat is the matrix C you want
Kai
Kai 2018 年 8 月 21 日
Thanks for your answer. However, this doesn't quite do what it should. Like this, XYZmat will be a 11 times 11 times 3 stack. What I need is a matrix of size 121 times 3, whose rows are the x- y- and z- coordinates of the points.
Julie
Julie 2018 年 8 月 21 日
Xtemp=X(coord);
Ytemp=Y(coord');
XYZmat=[Xtemp(:)';Ytemp(:)';M(:)'];
Kai
Kai 2018 年 8 月 21 日
Perfect, thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

Kai
2018 年 8 月 21 日

コメント済み:

Kai
2018 年 8 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by