Borrar filtros
Borrar filtros

Vector Indexing into 2D array

3 visualizaciones (últimos 30 días)
John Petersen
John Petersen el 5 de Mzo. de 2013
I have a 2D array and I want to pick out a different row value for each column.
Here is an example:
A = magic(3)
ans =
8 1 6
3 5 7
4 9 2
Pick out rows r=[2 3 2] from columns c=[1 2 3] Of course A(r,c) won't work, I just get the A reordered. What I want is the row vector
[A(r(1),c(1)) A(r(2),c(2)) A(r(3),c(3))]
which gives [3 9 7] .

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 5 de Mzo. de 2013
Editada: Azzi Abdelmalek el 5 de Mzo. de 2013
r=[2 3 2]
c=[1 2 3]
A=magic(3)
idx=sub2ind(size(A),r,c)
out=A(idx)

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by