How to extract matrix elements using index pairs?

12 ビュー (過去 30 日間)
Wendy Song
Wendy Song 2018 年 4 月 5 日
コメント済み: Wendy Song 2018 年 4 月 5 日
Might be a really basic question: Suppose I have a matrix:
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
I want to extract the elements A(1,1) and A(2,2). Is there a command to call the elements all at once by specifying a list of index pairs? Something like:
A((1,1);(2,2))
so that the output would be
ans =
8
5
I tried
A([1;2],[1;2])
A([1 1; 2 2])
but they both returned a matrix instead of a vector.

採用された回答

Birdman
Birdman 2018 年 4 月 5 日
r=[1 2];
c=[1 2];
A(sub2ind(size(A),r,c))
  1 件のコメント
Wendy Song
Wendy Song 2018 年 4 月 5 日
Awesome. Thanks!

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

その他の回答 (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