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 Commenti

Richard
Richard il 2 Mag 2012
could you provide an example?
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!

Accedi per commentare.

 Risposta accettata

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

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by