フィルターのクリア

How to index like A(B(i), i)

3 ビュー (過去 30 日間)
Hu
Hu 2013 年 10 月 17 日
回答済み: Azzi Abdelmalek 2013 年 10 月 17 日
For example,
A = rand(3, 5);
B = [3,2,1,2,3];
and I want C like:
for i = 1:size(A, 2)
C(i) = A(B(i), i);
end
Is there any way to get rid of the for-loop? For example, direct access like A(B(i), i).
I thought an anonymous function would help. While, if I define
f = @(x) (A(B(x), x))
C = f(1:size(A,2))
C will turn out to be a 5x5 matrix, while I want is a 1x5 matrix.
Is there any other solution?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 17 日
n=1:size(A,2);
C=A(sub2ind(size(A),B(n),n))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by