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.

采纳的回答

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