Error with accumarray code
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Star Strider
2020년 3월 25일
편집: Star Strider
2020년 3월 25일
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.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Weather and Atmospheric Science에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!