To find values by using position vector

Suppose matrix A=[1 3 5 7 9]. The position vector is known, which is, say, B=[ 3 4 ]. So the intended answer should be [ 5 7 ] which are elements at the 3rd and 4th places. Is there a function to achieve this? In fact the matrix A is high dimensional and there are many position vectors, and for loops seem not the most effective way...
Thanks in advance.

 採用された回答

the cyclist
the cyclist 2015 年 3 月 18 日

0 投票

answer = A(B)

1 件のコメント

Xia
Xia 2015 年 3 月 18 日
Thank you so much. I must be too stupid to ignore the basic indexing. Thanks for your help!

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

その他の回答 (1 件)

Guillaume
Guillaume 2015 年 3 月 18 日

0 投票

It's very basic matrix indexing:
A=[1 3 5 7 9];
B=[ 3 4 ];
C = A(B)
For N-d matrices, you may have to use sub2ind to go from coordinates to linear indices.

1 件のコメント

Xia
Xia 2015 年 3 月 18 日
Thank you Guillaume. You are right it's really basic and I learned from you. Again thanks for your time my friend!

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

Xia
2015 年 3 月 18 日

コメント済み:

Xia
2015 年 3 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by