Indexing 3D matrix with three 1D vector

1 回表示 (過去 30 日間)
Xin
Xin 2017 年 6 月 13 日
回答済み: Star Strider 2017 年 6 月 13 日
I have a 3D matrix, say A=100*100*100. I want to index 5 elements from it. What I have are three 1D vector (lenght=10), say x=[1,1,2,3,4] y=[55,22,33,55,77] z=[92,32,12,9,2]. I want to index the corresponding elements in the matrix A. Say [1,55,92] [1,22,32] [2,33,12] [3,55,9] [4,77,2]. Is there a very elegant way to index these five elemtents in one sentence?

採用された回答

Star Strider
Star Strider 2017 年 6 月 13 日
I would use the sub2ind (link) function. I am not certain you could do it in one line, but you can get close.
Example
M = randi(99, 4, 4, 3);
LinIdx = sub2ind(size(M), [1 2], [3 4], [2 3]);
Result = M(LinIdx);

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by