MATLAB Answers


Error: Cannot set support to 'positive' with non-positive data.

Asked by Tom Keaton on 30 May 2019
Latest activity Commented on by Tom Keaton on 30 May 2019
I continue to get the following error with my code:
Error using mvksdensity>compute_finite_support (line 274)
Cannot set support to 'positive' with non-positive data.
Error in mvksdensity (line 102)
[L,U] = compute_finite_support(support,ymin,ymax,d);
Error in ksdensity (line 226)
[fout,xout,u,plottype] = mvksdensity(yData,xi,varargin{:});
Error in collisions (line 537)
[f,xi] = ksdensity(cpdradial,'Support','positive','Censoring',cens,'NumPoints',10000);
I am running this code on data (attached file: 'dataset.met') to try and produce a density plot:
cpdcart = [];
for cpdtran2 = 1:particlecount
cpdcart = [cpdcart; collpos(cpdtran2).matrix(:,1:3)];
[cpdcol1,cpdrow1] = size(cpdcart);
cpdpolar = zeros(cpdcol1,2);
cpdradial = zeros(cpdcol1,1);
cpdphi = zeros(cpdcol1,1);
for cpdtran3 = 1:cpdcol1
cpdpolar(cpdtran3,1) = sqrt(cpdcart(cpdtran3,1).^2 + cpdcart(cpdtran3,2).^2 + cpdcart(cpdtran3,3).^2);
cpdradial(cpdtran3,1) = cpdpolar(cpdtran3,1);
cpdpolar(cpdtran3,2) = acos(cpdcart(cpdtran3,3)/cpdpolar(cpdtran3,1));
cpdphi(cpdtran3,1) = cpdpolar(cpdtran3,2);
%Plot collisions depending only on radius
cens = (cpdradial<0.02);
[f,xi] = ksdensity(cpdradial,'Support','positive','Censoring',cens,'NumPoints',10000);
xlabel 'Radial Distance (m) from Center of Sphere'
ylabel 'Collision Coordinate Density'
title({'Collision Coordinate Density vs Radial Distance',sprintf('Cross Section (m^2): %d',cs),sprintf('Temp (K): %d',ict),sprintf('Pressure (mTorr): %d',mTorr),sprintf('Time Span (sec): %d',tfin)})
hold on
grid on
grid minor
ax = gca;
ax.GridColor = 'w';
ax.MinorGridColor = 'w';
I know that my data is positive because I used the following for loop to determine if there were any negative values:
for checkneg = 1:164100
if cpdradial(checkneg,1) < 0
Which did not find any values less than negative. Any ideas why its throwing this error?


Sign in to comment.

1 Answer

Answer by the cyclist
on 30 May 2019
Edited by the cyclist
on 30 May 2019
 Accepted Answer

You've checked that your data are non-negative, not they're positive. I'm guessing that you have zeros, and the positive-support requirement means strictly positive, not just non-negative.


I see, that's right! Setting all 0-values to "NaN" did the trick. However, do you know if that will affect the ksdensity function plot if I do it that way or will it just ignore those places since they aren't considered numbers?
My intuition is that if you do not have a radical change in probability density between zero and very small values, then it should not be too bad as an approximation.
I suppose you could add a very tiny positive value to all your points, to nudge those zeros to positive, to see what the impact is.
Good idea, will try that. Thank you!

Sign in to comment.