Accessing a Matrix through an indices vector

조회 수: 6 (최근 30일)
Dukwhan
Dukwhan 2011년 2월 15일
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?

채택된 답변

Walter Roberson
Walter Roberson 2011년 2월 15일
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
Oleg Komarov
Oleg Komarov 2011년 2월 15일
I think in the first case:
A = [3,5,6];
B = rand(10,10,10);
idx = num2cell(A);
B(idx{:})
Dukwhan
Dukwhan 2011년 2월 15일
Yes, Oleg's version works well. Thank you both!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by