Error using trapz. Point spacing must be a scalar specifying uniform spacing or a vector of x-coordinates for each data point.
60 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 22 Jul 2022
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 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!