Expected input number 2, edges, to be one of these types

63 views (last 30 days)
Hello all,
Here I am doing a Chi-Square calculation
main
data = randi([0,10],1,20);
% dummy variable
% 10, 0.05, 'unif', 1.5, 2.9
n = 10;
alpha = 0.05;
dist = 'unif';
a1 = 1.5;
a2 = 2.9;
a3 = 3.0;
%Call to the function
[chi2, critical] = chi2test (data, n, alpha, dist, a1, a2);
The function is straigth forward, however the part I am have a issue is the prod, and the invcdf anonymous function. I do not have the tool kit for the cdf, so I did it by sprintf, using the histcounts. Here is where the issue lays, is there a way to get around this error?
function [chi2, critical] = chi2test (data, n, alpha, dist, a1, a2)
prob = @(a,b) sprintf('cdf(''%s'', b, %10.10g, %10.10g) - cdf(''%s'', a, %10.10g, %10.10g)', dist, a1, a2, dist, a1, a2);
invcdf = @(x) sprintf('icdf(''%s'', x, %10.10g, %10.10g)', dist, a1, a2);
pi = (1/n) .* (0:n);
intvls = invcdf(pi);
% The error is thrown here
o_freq = histcounts(data, intvls);
end
I recevie the following error
Error using histcounts
Expected input number 2, edges, to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64, logical
Thank you all for any help

Accepted Answer

Steven Lord
Steven Lord on 1 Feb 2021
function [chi2, critical] = chi2test (data, n, alpha, dist, a1, a2)
prob = @(a,b) sprintf('cdf(''%s'', b, %10.10g, %10.10g) - cdf(''%s'', a, %10.10g, %10.10g)', dist, a1, a2, dist, a1, a2);
invcdf = @(x) sprintf('icdf(''%s'', x, %10.10g, %10.10g)', dist, a1, a2);
This anonymous function does not call icdf on its inputs. It create a string that contains the icdf call that you would execute to compute the icdf. [The same holds for prob and cdf.] Don't use sprintf.
invcdf = @(x) icdf(dist, x, a1, a2);
If you really need the %10.10g behavior, use round on a1 and a2.
  2 Comments
SugerCodeCude
SugerCodeCude on 3 Feb 2021
Thank you Steven Lord, the fix on the anonymous function is alot clean, then for the issue on the error I get a matlab with the statistics needed to run chi-square directly.
I was looking to write the chi-square equation, however as time is of the essence I shell do that at a later time.
Thank you again

Sign in to comment.

More Answers (1)

dpb
dpb on 3 Feb 2021
W/O the Statistics TB, you'll have to resort to some other way to compute the wanted distribution quantities -- a quick look at File Exchange located <generalized-chi-square-distribution> that purports to do what you want. There may be others there.
  2 Comments
dpb
dpb on 3 Feb 2021
Answers is totally volunteer; there are a number like Steven who are TMW employees who contribute, but I am just a retired geezer... :)

Sign in to comment.

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!