Error in index returned by max() in the second dimension in obscure case
88 views (last 30 days)
Show older comments
Robert Kirkby
on 12 Sep 2024 at 5:39
Commented: Joss Knight
on 7 Oct 2024 at 8:29
Attaced is AAA.mat which contains the matrix that generates the issue, as well as a code that reproduces the problem.
Following, copied from code, shows what the issue is, essentially, if I do max in the first dimension of permute(AAA,[2,1,3,4]) then everything works fine. But if I do max in the second dimension of AAA ---which should give exact same answer--- then the max-values are reported correctly but the max-indexes are incorrect.
%% The problematic example
load AAA.mat
[vv1,ii1]=max(AAA,[],2);
[vv2,ii2]=max(permute(AAA,[2,1,3,4]),[],1);
squeeze(vv1)-squeeze(vv2) % all zeros as expected
squeeze(ii1)-squeeze(ii2) % many non-zeros, which should not happen
% Note: vv1 is correct, but ii1 is incorrect
% E.g., vv1(1,1,1,2)=-2.6733
% Which is correct, and the same as vv2(1,1,1,2)=-2.6733
% By comparison, ii1(1,1,1,2)=3081
% But the correct answer is ii2(1,1,1,2)=1
% We can see this because AAA(1,3081,1,2)=-5.1175, which is not vv1
% By comparison, AAA(1,1,1,2)=-2.6733, which is exactly what we expect it to be
% Unless I am missing something, [vv1,ii1]=max(AAA,[],2); is returning the incorrect index for the max.
% Note: (:,:,1,1) gets the indexes correct, but the indexes corresponding to most other 3rd and 4th
% dimension points, like (:,:,1,5) and (:,:,5,1) are wrong.
I don't know what about AAA causes this error to occur. The "max2derror.m" tries a bunch of things I thought of but they all work correctly.
Pretty sure I am getting this right :|
I ran this in R2023b.
0 Comments
Accepted Answer
Joss Knight
on 12 Sep 2024 at 14:52
Thank you! You have found a bug in MATLAB's GPU support. The computation of the indices is wrong when reducing along the rows and there are more than 2 dimensions and more than 2^13 columns. (In other words, the first answer is wrong, the second is correct.)
At the current time, there is no straightforward workaround except to use the CPU or permute the input and reduce down the columns.
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!