Getting different values when indexing with islocalmax and find

3 views (last 30 days)
I have a matrix of data A. and I want to find the maximum values in this matrix for each row. This is my script:
index_max_A = islocalmax(A,2);
Unrecognized function or variable 'A'.
max_A = A(index_max_A);
However, I find that this does not get me the maximum values. For reference I did:
for idx = 1: height(A)
max_A_2(idx) = A(idx, find(index_max_A(idx,:) == 1));
end
max_A_2 == max_A'
I dont get all logical 1. Can someone explain why that is?
I checked both max_A_2 and max_A, max_A_2 has the correct maximums.

Accepted Answer

Steven Lord
Steven Lord on 15 Nov 2022
The islocalmax function is not the correct function to use here. Note that in the code below that there are some rows of ind that contain no logical true values because there are no local maxima.
A = magic(4)
A = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
ind = islocalmax(A, 2)
ind = 4×4 logical array
0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
You want to use the max function, ideally asking it for linear indices.
[values, indices] = max(A, [], 2, 'linear')
values = 4×1
16 11 12 15
indices = 4×1
1 6 15 12
If we use these linear indices to change the values in A, you'll see that it changes the maximum entry in each row.
backupA = A % So you can see the difference easily
backupA = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
A(indices) = NaN
A = 4×4
NaN 2 3 13 5 NaN 10 8 9 7 6 NaN 4 14 NaN 1
If you need subscripts instead of linear indices, use ind2sub.
[row, column] = ind2sub(size(A), indices)
row = 4×1
1 2 3 4
column = 4×1
1 2 4 3
  2 Comments
Akshayaa Pandiyan
Akshayaa Pandiyan on 29 Nov 2022
How to I go around the islocalmax() if I have multiple maximum values?
Steven Lord
Steven Lord on 29 Nov 2022
Please describe exactly what results you're looking for. Consider this array:
A = magic(4);
A(2, 3) = 11
A = 4×4
16 2 3 13 5 11 11 8 9 7 6 12 4 14 15 1
What output would you expect and how would you compute that output? Don't worry about expressing it in code, just explain in words.

Sign in to comment.

More Answers (1)

Voss
Voss on 29 Nov 2022
The reason the elements of max_A_2 and max_A are not all equal has to do with how the results of indexing with a logical matrix are returned. Specifically, the elements are returned in column-major order (just like they are stored in memory). That is, you get all the elements corresponding to a true in the first column of the logical matrix, followed by all the elements corresponding to a true in the second column, and so on.
To illustrate with your data:
A = load('A.mat')
A = struct with fields:
Pout_r: [45×80 double]
A = A.Pout_r
A = 45×80
0.3285 0.3384 0.3479 0.3569 0.3652 0.3729 0.3799 0.3861 0.3916 0.3962 0.4000 0.4028 0.4048 0.4059 0.4060 0.4052 0.4035 0.4009 0.3974 0.3930 0.3878 0.3819 0.3751 0.3676 0.3595 0.3508 0.3415 0.3317 0.3215 0.3110 0.3226 0.3325 0.3420 0.3510 0.3594 0.3672 0.3743 0.3807 0.3864 0.3913 0.3953 0.3984 0.4007 0.4021 0.4026 0.4022 0.4009 0.3987 0.3956 0.3917 0.3869 0.3813 0.3750 0.3680 0.3602 0.3519 0.3430 0.3336 0.3238 0.3136 0.3168 0.3267 0.3361 0.3451 0.3536 0.3615 0.3688 0.3754 0.3813 0.3863 0.3906 0.3941 0.3967 0.3984 0.3993 0.3992 0.3983 0.3965 0.3938 0.3903 0.3860 0.3808 0.3749 0.3683 0.3610 0.3531 0.3446 0.3356 0.3261 0.3163 0.3110 0.3208 0.3303 0.3394 0.3479 0.3559 0.3633 0.3701 0.3761 0.3815 0.3860 0.3898 0.3927 0.3947 0.3959 0.3963 0.3958 0.3944 0.3921 0.3890 0.3851 0.3804 0.3749 0.3687 0.3618 0.3543 0.3462 0.3376 0.3285 0.3190 0.3052 0.3150 0.3245 0.3336 0.3422 0.3503 0.3579 0.3648 0.3711 0.3766 0.3814 0.3855 0.3887 0.3911 0.3927 0.3934 0.3932 0.3922 0.3904 0.3877 0.3842 0.3800 0.3749 0.3691 0.3627 0.3556 0.3479 0.3396 0.3309 0.3217 0.2995 0.3093 0.3188 0.3279 0.3366 0.3448 0.3525 0.3596 0.3660 0.3718 0.3769 0.3812 0.3848 0.3875 0.3894 0.3905 0.3908 0.3902 0.3887 0.3865 0.3834 0.3796 0.3750 0.3696 0.3636 0.3569 0.3496 0.3417 0.3333 0.3245 0.2939 0.3036 0.3131 0.3222 0.3310 0.3393 0.3471 0.3544 0.3610 0.3671 0.3724 0.3770 0.3809 0.3839 0.3862 0.3877 0.3883 0.3881 0.3871 0.3853 0.3827 0.3792 0.3751 0.3701 0.3645 0.3582 0.3513 0.3438 0.3358 0.3273 0.2883 0.2980 0.3074 0.3166 0.3254 0.3338 0.3418 0.3492 0.3561 0.3623 0.3679 0.3728 0.3770 0.3804 0.3830 0.3849 0.3859 0.3861 0.3855 0.3841 0.3819 0.3789 0.3752 0.3707 0.3655 0.3596 0.3531 0.3460 0.3383 0.3302 0.2827 0.2924 0.3018 0.3110 0.3199 0.3284 0.3365 0.3441 0.3512 0.3576 0.3635 0.3687 0.3732 0.3769 0.3799 0.3821 0.3835 0.3842 0.3840 0.3830 0.3812 0.3787 0.3753 0.3713 0.3665 0.3610 0.3549 0.3482 0.3409 0.3331 0.2772 0.2868 0.2963 0.3055 0.3144 0.3231 0.3313 0.3390 0.3463 0.3530 0.3591 0.3646 0.3694 0.3735 0.3768 0.3794 0.3812 0.3822 0.3825 0.3819 0.3806 0.3785 0.3756 0.3719 0.3675 0.3625 0.3568 0.3504 0.3435 0.3361
plot(A.')
index_max_A = islocalmax(A,2)
index_max_A = 45×80 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Notice that index_max_A is all false in the first 14 columns:
any(index_max_A(:,1:14),'all')
ans = logical
0
and that the 15th column has 6 elements that are true:
nnz(index_max_A(:,15))
ans = 6
which are in rows 1-3 and 31-33:
find(index_max_A(:,15))
ans = 6×1
1 2 3 31 32 33
Those 6 true elements in the 15th column correspond to the first 6 elements you get when you do this:
max_A = A(index_max_A)
max_A = 45×1
0.4060 0.4026 0.3993 0.4060 0.4026 0.3993 0.3963 0.3934 0.3963 0.3934
See:
temp = A(find(index_max_A(:,15)),15) % 1st 6 elements of max_A come from the 15th column
temp = 6×1
0.4060 0.4026 0.3993 0.4060 0.4026 0.3993
isequal(temp,max_A(1:6))
ans = logical
1
The point is that max_A contains the elements of A that are local maxima, in column order, but you define max_A_2 to be in row order. That's why they're different (but the first three elements are the same because they happen to occur in the first three rows).
To have max_A be in row order, so to be consistent with max_A_2, index the transpose of A with the transpose of the logical matrix:
A_temp = A.';
max_A = A_temp(index_max_A.')
max_A = 45×1
0.4060 0.4026 0.3993 0.3963 0.3934 0.3908 0.3883 0.3861 0.3842 0.3825
Now max_A is the same as max_A_2:
for idx = 1: height(A)
max_A_2(idx) = A(idx, find(index_max_A(idx,:) == 1));
end
max_A_2 == max_A'
ans = 1×45 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
isequal(max_A_2,max_A')
ans = logical
1

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!