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 Center および File ExchangeCreating, 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