Question

I'm trying to do something like the following code:

k = linspace(a,b);
x = c:0.01:d;
% k and x are of different sizes
f = @(s) arrayfun(@(t) normcdf(s, b0+b1*t, sigma), x);
y = arrayfun(f, k);

I get the following error

Error using arrayfun Non-scalar in Uniform output, at index 1, output 1. Set 'UniformOutput' to false.

I'm trying to avoid using a for loop for each element in k.
Also, for each result matching an element in k, I need to do another small calculation

Example with a loop:

for i=1:m  % m is the number of elements in k
    f = @(t) normcdf(k(i), b0+b1*x, sigma);
    y = arrayfun(f, x);
    res(i) = trapz(x,y);
end

any idea how can I get the same result as the for loop with the first method?
and why am I getting the error?

Was it helpful?

Solution

I would suggest the following:

k = linspace(a,b);
x = c:0.01:d;
[X,K] = meshgrid(x,k)

y = arrayfun(@(p,t) normcdf(p, b0+b1*t, sigma), K(:), X(:))
res = cumtrapz(x,y)

Untested though as you gave no example data and desired results. Maybe you need to swap the order of x and k as well as X and K to get the desired result. (or use ndgrid instead of meshgrid, has the same effect)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top