Accessing a Matrix through an indices vector

6 visualizaciones (últimos 30 días)
Dukwhan
Dukwhan el 15 de Feb. de 2011
I have a linear vector of dimensional indices (A) and would like to access a single element from matrix M using this vector.
e.g.
A = [3,5,6];
FUNC(m,A); // equivalent to m(3,5,6);
Is there a method in MATLAB that replaces FUNC that allows this?

Respuesta aceptada

Walter Roberson
Walter Roberson el 15 de Feb. de 2011
EDIT: fixed per Oleg's catch of my mistake:
t = num2cell(A);
m(t{:})
Or if you prefer one-line answers better:
subsref(m, struct('type', repmat({'()'},1,length(A)), ...
'value', num2cell(A)))
  2 comentarios
Oleg Komarov
Oleg Komarov el 15 de Feb. de 2011
I think in the first case:
A = [3,5,6];
B = rand(10,10,10);
idx = num2cell(A);
B(idx{:})
Dukwhan
Dukwhan el 15 de Feb. de 2011
Yes, Oleg's version works well. Thank you both!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by