フィルターのクリア

Extract data from matrices and arrays using vectors of coordinates

71 ビュー (過去 30 日間)
Ariel Kessel Akerman
Ariel Kessel Akerman 2021 年 9 月 28 日
I have a matrix and I want to use two vectors of row and column index coordinates in order to extract the elements from it. In particular, I can define a 3 x 3 matrix as
A = [1 2 3; 3 4 5; 8 9 10]
And I want to use two vectors to extract the elements (2,2), (3,1), (1,3). My intention is to do this using the vectors for each corrdinates. I have been trying to do it by writing
A([2 3 1], [2 1 3])
but of course this is not working. I believe that this is a basic sintax issue.
Thank you so much

採用された回答

the cyclist
the cyclist 2021 年 9 月 28 日
編集済み: the cyclist 2021 年 9 月 28 日
Use the sub2ind function to convert the subscripts to a linear index into the matrix.
Using your example:
A = [1 2 3; 3 4 5; 8 9 10]
A = 3×3
1 2 3 3 4 5 8 9 10
linearIndex = sub2ind(size(A),[2 3 1], [2 1 3])
linearIndex = 1×3
5 3 7
A(linearIndex)
ans = 1×3
4 8 3

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by