Question

'Outliers.m' is called from a higher level .m file. The variables are all defined in the higher level file, and set as globals for access by Outliers.m. The purpose of the code is to identify outliers using Chauvenets Criterion, and for this, I have to calculate the integral of the guassian distribution, using the Integral function and function handles. The code works and gives sensible values when I enter specific variables as a test, but I cannot get it to work in a loop. My data set is comprised of 7 individual samples, each 1x30, all of which need to be analyzed. I have had various errors, read through the guidance on Integral and function handles, but cannot seem to find the solution...Any help or guidance would be very much appreciated.... Here is my code:

n = 7
for x = 1:n
    for y = 1:30
    z(x,y) = abs((cc(x,y) - mastercc(1,y))/masterccstd(1,y));
    xmax(x,y) = mastercc(1,y)+z(x,y)*masterccstd(1,y);
    xmin(x,y) = mastercc(1,y)-z(x,y)*masterccstd(1,y);
    p(x,y) = 1/(masterccstd(1,y)*(sqrt(2*pi)));

    fun(x,y)= @(x,y,z) (exp(-1/2)*z(x,y).^2);
    q(x,y) = integral(fun(x,y),xmin(x,y),xmax(x,y),'ArrayValued',true);

    pq(x,y) = p(x,y)*q(x,y); % probability
    value(x,y) = n*(1/pq(x,y));
    count(x,y) = logical(value(x,y) <0.5);
    badbins(x)=sum(count(x,:));
    end
end
Was it helpful?

Solution 2

Solution to the loop problem, courtesy Andrei Bobrov via Matlab Central, link below:

http://www.mathworks.com/matlabcentral/answers/103958#comment_177000

NB: Please note the code is not complete for the purpose I explained in the problem description, but it does solve the Loop error.

OTHER TIPS

It seems like your error is caused by an invald function definition.

If you try it like this it should work:

fun = @(x,y,z) (exp(-1/2)*z(x,y).^2)

Now it can be called like this for example:

fun(1,2,magic(4))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top