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!