matrix indexing without loop

2 次查看(过去 30 天)
Hello,
Suppose I have two matrices, A and B:
A = [2 3 1 4;
1 4 1 3];
B = [1 2 3 4;
4 3 2 1];
Now I get the max indices for the matrix A:
[~,ind] = max(A,[],2);
I want to obtain the corresponding values of matrix B with ind:
for ii = 1:2
v(ii) = B(ii,ind(ii));
end
How to get the values of B without a for loop?

采纳的回答

Walter Roberson
Walter Roberson 2018-8-2
v = B(sub2ind(size(B), 1:size(B,1), ind))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by