如何得到361x91数组中重复最大值的位置

最大值是两个重复的值,要得到两个的位置

 Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 8 Mai 2024

1 vote

Use find -
%Sample data
mat = magic(4);
mat = [mat; 1 4 9 16]
mat = 5x4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 1 4 9 16
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%Get the maximum value
val = max(mat, [], 'all')
val = 16
%% Find the indices of all occurences of the max value
%Linear indices
idx = find(mat==val)
idx = 2x1
1 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%Subscript indices
[r,c] = find(mat==val)
r = 2x1
1 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
c = 2x1
1 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note - Use tolerance to compare for floating point values.

Plus de réponses (0)

Catégories

Produits

Version

R2023b

Tags

Community Treasure Hunt

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

Start Hunting!