Using a matrix as pairs of indexes of another matrix

6 ビュー (過去 30 日間)
bbb_bbb
bbb_bbb 2019 年 7 月 4 日
編集済み: Stephen23 2019 年 7 月 4 日
For instance:
x=[
1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90
100 200 300 400 500 600 700 800 900];
% idx are coordinates (row,col) of x array
idx=[1 3
2 5
3 8];
for i=1:size(idx,1) % cycle through pairs of idx
y(i)=x(idx(i,1),idx(i,2));
end
y
y =
3 50 800
How can this be done without a loop?

採用された回答

Stephen23
Stephen23 2019 年 7 月 4 日
編集済み: Stephen23 2019 年 7 月 4 日
"How can this be done without a loop?"
Using sub2ind, which converts to linear indices:
>> ind = sub2ind(size(x),idx(:,1),idx(:,2));
>> x(ind)
ans =
3
50
800

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by