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 comentarios

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

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 2 de Mayo de 2012
YourMatrix( sub2ind( size(YourMatrix), Rows, Columns ) )

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by