get specific elements from matrix

Hello,
From an arbitrary matrix I want to extract n elements when I have 2 arrays of lenth n, one containing the row indices and 1 containing the column indices of the desired elements.
For effeciency I would like to avoid a for loop. Is this possible?
Thanks!

2 件のコメント

Richard
Richard 2012 年 5 月 2 日
could you provide an example?
Erik de Boer
Erik de Boer 2012 年 5 月 2 日
Sure
Say I have a matrix A =
[3 6 2
5 1 3
4 7 8]
Row vector R = [1 2 3] and column vector C = [3 1 1]
Then I want to etract the elements [1,3],[2,1],[3,1] and arrive at
ans = [2 5 4]
Walter Robertson's aswer does exactly that. Thanks!

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

 採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 2 日

2 投票

YourMatrix( sub2ind( size(YourMatrix), Rows, Columns ) )

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by