Getting values of 3D matrix via their (x,y) coordinates without using loop

3 ビュー (過去 30 日間)
Konstantinos
Konstantinos 2016 年 6 月 29 日
コメント済み: José-Luis 2016 年 6 月 29 日
It might be trivial. I have a matrix say A which is 3D and consists of two slices ( A(:,:,1) and A(:,:,2)). I want to get a vector containing the values of a slice of this matrix (say the first slice) that correspond to the (x,y) coordinates I have in two row vectors, say: points_x and points_y i.e. I want to get the values:
A(points_x(1), points_y(1), 1), A(points_x(2), points_y(2), 1), ..., A(points_x(end), points_y(end), 1)
but I cannot figure out how to do this. I have tried:
A([points_x; points_y], 1)
but it returned a rectangular matrix not a vector. I don't want to use a for loop for the sake of code performance. Any ideas?
  2 件のコメント
José-Luis
José-Luis 2016 年 6 月 29 日
If you want a vector, how are you going to distinguish between points?
Konstantinos
Konstantinos 2016 年 6 月 29 日
If the vector contains the values corresponding to the 2D coordinates in order which is:
A(points_x(1), points_y(1), 1), A(points_x(2), points_y(2), 1), ..., A(points_x(end), points_y(end), 1)
I am OK with that and I don't need to distringish them

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

採用された回答

José-Luis
José-Luis 2016 年 6 月 29 日
A = rand(5,5,2);
x = (1:3);
y = (2:4);
your_result = cell2mat(arrayfun(@(x,y) reshape(A(x,y,:),1,[]),x,y,'uniformoutput',false))
  2 件のコメント
Konstantinos
Konstantinos 2016 年 6 月 29 日
Thank you ver much. The odd indices correspond to the 1st slice while the even indices correspond to the 2nd slice.
José-Luis
José-Luis 2016 年 6 月 29 日
My pleasure.

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

その他の回答 (1 件)

KSSV
KSSV 2016 年 6 月 29 日
why dont you go for interpolation? Interpolation would be the easiest one to get values at your desired points.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by