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 commentaires

Richard
Richard le 2 Mai 2012
could you provide an example?
Erik de Boer
Erik de Boer le 2 Mai 2012
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!

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 2 Mai 2012

2 votes

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

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by