sub2ind Get values of 3D matrix using an index array?

11 ビュー (過去 30 日間)
Penny Less
Penny Less 2019 年 9 月 10 日
コメント済み: Penny Less 2019 年 9 月 11 日
Hi all,
I have a 800x900x7 matrix (matrix M) and a 800x900 2D array (matrix A) whose elements are the indexes i want to obtain from each row and column in matrix M (so each element in array A is a number from 1 to 7).
The final result should be an 800x900 matrix (matrix F) containing the elements in matrix M which matrix A specifies.
Without using a for loop, how am I able to do this? I'm thinking sub2ind, but any help would be greatly appreciated!
Many thanks
  1 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 10 日
You mean A matrix are indices? Could you illustrate with 2by2 A matrix and a 2by2by2 M matrix?

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 9 月 10 日
[m,n,k] = size(M);
[q,w] = ndgrid(1:m,1:n);
out = M(sub2ind([m,n,k],q,w,A));
  1 件のコメント
Penny Less
Penny Less 2019 年 9 月 11 日
Works perfectly, thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by