How to compare some specific entries of a matrix, if I have their indices stored in 2 files?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have 2 files, that have numbers that represent rows and columns respectively, out of which I want to pick a pair at a time, and compare that corresponding element from a given matrix. For example, if I have matrices A and B, such that:-
A= [1,5,9] and B= [2,6,11]
Then I want to compare the (1,2), (5,6) and (9,11) elements of a matrix C, and return the indices of the largest value.
Originally, my matrix A and B are 19007x1 in size. And my matrix C is 5601x5601 in size.
댓글 수: 0
채택된 답변
José-Luis
2017년 7월 7일
idx = sub2ind(size(C), A, B)
result = max(C(idx));
댓글 수: 7
Guillaume
2017년 7월 7일
편집: Guillaume
2017년 7월 7일
The problem is with the find(C == max(C(idx))), you can't use find on the full C matrix, you have to do it on the subset indexed by idx, so:
find(C(idx) == max(C(idx)))
edit, after José-Luis edit while I was writing my comment: while the new code may provide correct results most of the time, this is still wrong. As it searches the whole C matrix, not just the subset provided by A and B. Try with example:
A = 1;
B = 1;
C = [0 0; 0 0];
추가 답변 (1개)
Guillaume
2017년 7월 7일
As per José-Luis' answer, you have to use sub2ind to convert your 2D indexing from A and B into linear indices. This would work:
index = sub2ind(size(C), A, B);
[~, row] = max(C(index));
row is the row in A and B where the maximum is found. If there are several locations where it is found, then you only get the first one. If you want all of them:
Csearch = C(sub2ind(size(C), A, B));
rows = find(Csearch == max(Csearch));
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!