Error using trapz. Point spacing must be a scalar specifying uniform spacing or a vector of x-coordinates for each data point.

조회 수: 59 (최근 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.

채택된 답변

Voss
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)
ans = 0.4894
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)
Error using trapz
Point spacing must be a scalar specifying uniform spacing or a vector of x-coordinates for each data point.
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개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by