Error in index returned by max() in the second dimension in obscure case

1 回表示 (過去 30 日間)
Robert Kirkby
Robert Kirkby 2024 年 9 月 12 日
コメント済み: Joss Knight 2024 年 10 月 7 日
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.

採用された回答

Joss Knight
Joss Knight 2024 年 9 月 12 日
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.
  2 件のコメント
Robert Kirkby
Robert Kirkby 2024 年 9 月 15 日
Thanks! I'll just use the permute() trick for now and look forward to a future fix :D
That the trigger was having more than 2^13 columns makes me feel much better about being unable to figure out what it was when trying some simple examples :)
Joss Knight
Joss Knight 2024 年 10 月 7 日
You can now follow the fix and find workarounds with this MATLAB Bug Report.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by