Error using trapz. Point spacing must be a scalar specifying uniform spacing or a vector of x-coordinates for each data point.
조회 수: 60 (최근 30일)
이전 댓글 표시
Plot_X = 0:0.01:1;
Per = quantile(SUSC_MO,1:-0.01:0);
for i = 1:101
if i <101
Plot_Y(i) = sum(CAT_M(SUSC_MO>=Per(101-i))) / sum(CAT_M,'all');
else
Plot_Y(i) = sum(CAT_M(SUSC_MO>=0)) / sum(CAT_M,'all');
end
end
AUC= round(100*trapz(Plot_X,Plot_Y),1);
the error is in line 10.
댓글 수: 0
채택된 답변
Voss
2022년 7월 22일
When X and Y are the same length
X = 0:0.01:1;
Y = rand(1,101);
trapz works
trapz(X,Y)
But when X and Y are not the same length
X = 0:0.01:1;
Y = rand(1,102);
the error you saw happens
trapz(X,Y)
Therefore, it may be that your Plot_Y is not the same length as your Plot_X. (Perhaps Plot_Y has extra elements from a previous run of your script, for instance, when Plot_X had more than 101 elements.)
If that is the problem, you can correct it by pre-allocating Plot_Y before your for loop:
Plot_X = 0:0.01:1;
% ...
Plot_Y = zeros(size(Plot_X)); % pre-allocate Plot_Y to be a vector of zeros the same size as Plot_X
for i = 1:numel(Plot_X)
% ...
end
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!