[英]use 2Xn matrix entries as indices
是否可以使用矩陣條目作為其他矩陣的索引? 例如 :
A=[1 2 ; 4 5 ; 6 7 ];
我想使用A而不使用循環來訪問其他矩陣的條目。
Othermat(1,2), Othermat(4,5) %...
如果是,我該怎么辦?
當然,請使用sub2ind
:
A = [1 2; 4 5; 6 7];
ind = sub2ind(size(Othermat),A(:,1),A(:,2));
values = Othermat(ind);
建議的sub2ind
是生成索引的自然方法。
當然直接找到線性索引也不是很困難:
A = [1 2; 4 5; 6 7];
Othermat = magic(7);
Othermat(A(:,1)+(A(:,2)-1)*size(Othermat,1))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.