[英]Accessing all elements of a matrix in MATLAB without using 'for' loops
[英]Octave/MATLAB: Using a matrix to access elements in a matrix without loops
考慮以下兩個矩陣:
>> columns = [1,3,2,4]
和
>> WhichSet =
[2, 2, 1, 2;
1, 1, 2, 1;
1, 2, 1, 2;
2, 1, 2, 2]
我的意圖是執行以下操作 :
>> result = [WhichSet(1,columns(1)), WhichSet(2,columns(2)), WhichSet(3, columns(3)) and WhichSet(4, columns(4))]
result = [2,2,2,2]
沒有任何循環。
由於索引的工作方式,除非您使用線性索引 ,否則您不能只是像現在那樣插入它們
您需要的線性指數為:
ind=sub2ind(size(WhichSet),1:size(whichSet,1),columns);
然后
out=WhichSet(ind);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.