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일

0 개 추천

v = B(sub2ind(size(B), 1:size(B,1), ind))

댓글 수: 1

By the way, it should be:
v = B(sub2ind(size(B), (1:size(B,1))', ind))
Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2018년 8월 2일

댓글:

2018년 8월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by