MATLAB: Error with accumarray code

accumarayerror

So I have matlab code to find maximum wave height (Hs) with 2 condition: by direction and by year like this photos below.
TableHm2 = readtable('02_Output HsTs.xlsx');
[r,rn] = findgroups(TableHm2(:,1));
[c,cn] = findgroups(TableHm2(:,6));
out = accumarray([r,c],TableHm2.Hs,[],@max);
Tout = array2table([rn.Tahun,out],'VariableNames',[{'Year'};cn.Arah2]);
But when I was running, it didnt work. The error says:
Error using accumarray
First input SUBS must contain positive integer subscripts.
Error in coba3 (line 4)
out = accumarray([r,c],TableHm2.Hs,[],@max);
So, how should I do? Thanks before

Best Answer

For some reason, accumarray doesn’t like the findgroups ‘G’, at least here. I have no idea why it throws those errors, since all the ‘G’ elements are positive integers. Even using the fix function to force them to be integers otherwise fails to solve that problem.
The splitapply function has no problems with them:
TableHm2 = readtable('02_Output HsTs.xlsx');
[G,ID] = findgroups(TableHm2(:,[1 6]));
Out = splitapply(@max, TableHm2{:,9}, G);
Tout = table(ID{:,1},ID{:,2},Out, 'VariableNames',{ID.Properties.VariableNames{1:2},'Hs'});
This runs without error (in R2020a).
EDIT — (25 Mar 2020 at 20:36)
I found the problem with accumarray — there is NaN value in ‘G’.
This modified accumarray call works, and gives the same result as the splitapply call:
Out2 = accumarray(G(isfinite(G)), (1:size(G(isfinite(G)),1)).', [], @(x)max(TableHm2{x,9}));
The rest of the code is unchanged.