Error in index returned by max() in the second dimension in obscure case
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.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. 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. max MATLAB Answers — New Questions