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 で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!