Borrar filtros
Borrar filtros

Access element of ND array specified by another array?

1 visualización (últimos 30 días)
Rohit Gupta
Rohit Gupta el 7 de Jun. de 2018
Comentada: Rohit Gupta el 7 de Jun. de 2018
I have a N1 x N2 x N3 dimension Tensor T. Plus I have a 1 x 3 array a = [a1 a2 a3]. How do I access element T(a1,a2,a3). Is there any way to do this for N dimension.

Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Jun. de 2018
Editada: Walter Roberson el 7 de Jun. de 2018
acell = num2cell(a);
T(acell{:})
There are also ways to calculate the location as a linear index based upon size(T)
Ts = size(T):
La = length(a);
Ts(end+1:La) = 1;
idx = a(1) + sum((a(2:end)-1) .* cumprod(Ts(1:La-1)))
T(idx)
... if I didn't miss something.
  1 comentario
Rohit Gupta
Rohit Gupta el 7 de Jun. de 2018
Thanks! I wanted to avoid doing something you mentioned below. First solution works.

Iniciar sesión para comentar.

Más respuestas (1)

James Tursa
James Tursa el 7 de Jun. de 2018
  1 comentario
Rohit Gupta
Rohit Gupta el 7 de Jun. de 2018
Thanks! But it also needs giving a1,a2,a3 and wont work for any general dimension.

Iniciar sesión para comentar.

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