필터 지우기
필터 지우기

Access element of ND array specified by another array?

조회 수: 1 (최근 30일)
Rohit Gupta
Rohit Gupta 2018년 6월 7일
댓글: Rohit Gupta 2018년 6월 7일
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.

채택된 답변

Walter Roberson
Walter Roberson 2018년 6월 7일
편집: Walter Roberson 2018년 6월 7일
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
Rohit Gupta
Rohit Gupta 2018년 6월 7일
Thanks! I wanted to avoid doing something you mentioned below. First solution works.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

James Tursa
James Tursa 2018년 6월 7일
  댓글 수: 1
Rohit Gupta
Rohit Gupta 2018년 6월 7일
Thanks! But it also needs giving a1,a2,a3 and wont work for any general dimension.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by