Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Is it possible to convert this for loop into a matrix?

Subject: Is it possible to convert this for loop into a matrix?

From: Haoyang Liu

Date: 27 Dec, 2010 21:18:04

Message: 1 of 14

I have the following for loop:

N=5000;
a=rand(N,1); %in the actual program, this is not rand, but rand would do for now
b=rand(N,1);
c=zeros(N);
for i = 1:N
    for j = 1:N
        if a(i)+b(j) >= threshold1
             d=f1(a(i),b(j));
        elseif a(i)+b(j) <= threshold2
             d=f2(a(i),b(j));
        end
        c(i,j)=f(a(i),b(j))/d;
    end
end

This takes a very very long time to run. I'd like to be able to speed it up via using matrix operation or other means. I should note that the inline functions in the above code are non-linear functions of x and y. Essentially I need to populate the matrix with a function whose input depends on the position of matrix in reference to another vector.

Any help is greatly appreciated.

Subject: Is it possible to convert this for loop into a matrix?

From: Walter Roberson

Date: 27 Dec, 2010 21:37:31

Message: 2 of 14

On 27/12/10 3:18 PM, Haoyang Liu wrote:
> I have the following for loop:
>
> N=5000;
> a=rand(N,1); %in the actual program, this is not rand, but rand would do
> for now
> b=rand(N,1);
> c=zeros(N);
> for i = 1:N
> for j = 1:N
> if a(i)+b(j) >= threshold1
> d=f1(a(i),b(j));
> elseif a(i)+b(j) <= threshold2
> d=f2(a(i),b(j));
> end
> c(i,j)=f(a(i),b(j))/d;
> end
> end
>
> This takes a very very long time to run. I'd like to be able to speed it
> up via using matrix operation or other means. I should note that the
> inline functions in the above code are non-linear functions of x and y.
> Essentially I need to populate the matrix with a function whose input
> depends on the position of matrix in reference to another vector.
>
> Any help is greatly appreciated.

a_b = bsxfun(@plus, a, b.');
f1pos = a_b >= threshold1;
f2pos = ~f1pos & a_b <= threshold2
fab = bsxfun(@f, a, b.');
f1ab = bsxfun(@f1, a, b.');
f2ab = bsxfun(@f2, a, b');
fab(f1pos) = fab(f1pos) ./ f1ab(f1pos);
fab(f2pos) = fab(f2pos) ./ f2ab(f2pos);
fab(~(f1pos | f2pos)) = NaN;

The NaN corresponds to the fact that you have not defined d for the case
where the sum does not fall within either threshold.

Probably the performance could be improved -- if f or f1 or f2 can be
vectorized, or if there is no third undefined possibility.

Subject: Is it possible to convert this for loop into a matrix?

From: Haoyang Liu

Date: 27 Dec, 2010 21:58:05

Message: 3 of 14

My mistake. There is only one threshold. I'm not sure why I had threshold1 and threshold2, they are the same.

Do you have any pointers as to how I can vectorize f,f1,and f2?

f = constant*(x+y)*(1/x+1/y)
f1 = exp(V_max(x,y))
and V_max is the local maximum of a non-linear algebraic function of x and y?

Thanks for your quick response.

Walter Roberson <roberson@hushmail.com> wrote in message <wG7So.18696$My1.7287@newsfe16.iad>...
> On 27/12/10 3:18 PM, Haoyang Liu wrote:
> > I have the following for loop:
> >
> > N=5000;
> > a=rand(N,1); %in the actual program, this is not rand, but rand would do
> > for now
> > b=rand(N,1);
> > c=zeros(N);
> > for i = 1:N
> > for j = 1:N
> > if a(i)+b(j) >= threshold1
> > d=f1(a(i),b(j));
> > elseif a(i)+b(j) <= threshold2
> > d=f2(a(i),b(j));
> > end
> > c(i,j)=f(a(i),b(j))/d;
> > end
> > end
> >
> > This takes a very very long time to run. I'd like to be able to speed it
> > up via using matrix operation or other means. I should note that the
> > inline functions in the above code are non-linear functions of x and y.
> > Essentially I need to populate the matrix with a function whose input
> > depends on the position of matrix in reference to another vector.
> >
> > Any help is greatly appreciated.
>
> a_b = bsxfun(@plus, a, b.');
> f1pos = a_b >= threshold1;
> f2pos = ~f1pos & a_b <= threshold2
> fab = bsxfun(@f, a, b.');
> f1ab = bsxfun(@f1, a, b.');
> f2ab = bsxfun(@f2, a, b');
> fab(f1pos) = fab(f1pos) ./ f1ab(f1pos);
> fab(f2pos) = fab(f2pos) ./ f2ab(f2pos);
> fab(~(f1pos | f2pos)) = NaN;
>
> The NaN corresponds to the fact that you have not defined d for the case
> where the sum does not fall within either threshold.
>
> Probably the performance could be improved -- if f or f1 or f2 can be
> vectorized, or if there is no third undefined possibility.

Subject: Is it possible to convert this for loop into a matrix?

From: Matt Fig

Date: 27 Dec, 2010 22:21:05

Message: 4 of 14

"Haoyang Liu" wrote in message <ifb25d$ihh$1@fred.mathworks.com>...
> My mistake. There is only one threshold. I'm not sure why I had threshold1 and threshold2, they are the same.
>
> Do you have any pointers as to how I can vectorize f,f1,and f2?


Well that would depend on what is in f, f1 and f2 of course. If there is only one threshold, then you don't need an ELSEIF, you just need an ELSE.

Subject: Is it possible to convert this for loop into a matrix?

From: Walter Roberson

Date: 27 Dec, 2010 22:54:39

Message: 5 of 14

On 27/12/10 3:58 PM, Haoyang Liu wrote:

> Do you have any pointers as to how I can vectorize f,f1,and f2?
>
> f = constant*(x+y)*(1/x+1/y)
> f1 = exp(V_max(x,y))
> and V_max is the local maximum of a non-linear algebraic function of x
> and y?

First pass, assuming column vectors

f = @(x,y) constant .* bsxfun(@(x1,y1) (x1+y1)./(1./x1 + 1./y1), x, y.');


Second pass, assuming column vectors, might be slower:

function res = f(x,y)
   flipped = false;
   if size(x,1) < size(y,1)
     t = y;
     y = x;
     x = t;
     flipped = true;
   end
    numx = size(x,1);
    numy = size(y,1);
    res = zeros(numx, numy);
    xrecip = 1./x;
    for K = 1 : numy
      res(:,K) = (x + y(K)) * (xrecip + 1./y(K));
    end
    res = constant .* res;
    if flipped; res = res.'; end
end


This second pass should use far fewer arithmetic operation calls and far
fewer function calls, and may be able to use BLAS to run parts in
parallel since your vectors are sufficiently long. On the other hand, it
does use more matlab code and matlab code that is _theoretically_ more
efficient can turn out to be much slower than built-in functions.

You can remove the bit about flipping if x and y will always be the same
size, or if x is always longer than y. The purpose of the flipping is to
have the vectorized part of the loop operate on the longest vector. This
kind of flipping is only suitable when the operations are symmetric in
the variables, as is the case here. (Also, if the vector sizes are not
so different, the time and memory taken to do the transpose might not be
worthwhile, so you if you keep the flipping, you might want to use a
fudge-factor to hold off flipping until it would gain more than the
transpose loses.)

Subject: Is it possible to convert this for loop into a matrix?

From: Walter Roberson

Date: 27 Dec, 2010 23:08:27

Message: 6 of 14

On 27/12/10 3:58 PM, Haoyang Liu wrote:

> Do you have any pointers as to how I can vectorize f,f1,and f2?
>
> f = constant*(x+y)*(1/x+1/y)
> f1 = exp(V_max(x,y))
> and V_max is the local maximum of a non-linear algebraic function of x
> and y?

We need more information about what you mean by "the local maximum" of a
function. Do you mean that V_max has a formula in 2D that might have
several maxima, that the formula should be searched for the "closest"
maxima to (x,y), and the value of the formula at that closest maxima
should be returned? If so, then what if there are multiple maxima at the
same distance? Is the search only to be along one of the axis or in 2D
or along some particular vector ? Is it okay if the local maxima so
found is "wimpy" compared to another local maxima that is slightly
further away? Or should the partial derivatives along x and y be
calculated and the maxima chosen be the largest of the maxima in the
four directions (+x, -x, +y, -y)? Or .... ??

If this general idea of searching for a maxima is what you want to do,
then is it correct that different (x,y) might lead to the same maxima
point because those points are all on the "hillside" of that particular
maxima ? Or is the function so non-linear as for that to be implausible?
If it is not implausible, then are the hillsides sufficiently large as
for it to make sense that once a local maxima is found, to look around
it to the boundaries of its influence, and to "fill in" the table for
all the (x,y) that are on that hillside, making it unnecessary to do the
search for those points ?

Subject: Is it possible to convert this for loop into a matrix?

From: Haoyang Liu

Date: 27 Dec, 2010 23:27:04

Message: 7 of 14

Thanks for your help.

Actually, for a given x and y, V (as a function of another variable, say z), has only one maximum. But the position (w.r.t. z) and value of the maximum is different depending on the x,y pair.

Walter Roberson <roberson@hushmail.com> wrote in message <M%8So.22950$DO.13893@newsfe09.iad>...
> On 27/12/10 3:58 PM, Haoyang Liu wrote:
>
> > Do you have any pointers as to how I can vectorize f,f1,and f2?
> >
> > f = constant*(x+y)*(1/x+1/y)
> > f1 = exp(V_max(x,y))
> > and V_max is the local maximum of a non-linear algebraic function of x
> > and y?
>
> We need more information about what you mean by "the local maximum" of a
> function. Do you mean that V_max has a formula in 2D that might have
> several maxima, that the formula should be searched for the "closest"
> maxima to (x,y), and the value of the formula at that closest maxima
> should be returned? If so, then what if there are multiple maxima at the
> same distance? Is the search only to be along one of the axis or in 2D
> or along some particular vector ? Is it okay if the local maxima so
> found is "wimpy" compared to another local maxima that is slightly
> further away? Or should the partial derivatives along x and y be
> calculated and the maxima chosen be the largest of the maxima in the
> four directions (+x, -x, +y, -y)? Or .... ??
>
> If this general idea of searching for a maxima is what you want to do,
> then is it correct that different (x,y) might lead to the same maxima
> point because those points are all on the "hillside" of that particular
> maxima ? Or is the function so non-linear as for that to be implausible?
> If it is not implausible, then are the hillsides sufficiently large as
> for it to make sense that once a local maxima is found, to look around
> it to the boundaries of its influence, and to "fill in" the table for
> all the (x,y) that are on that hillside, making it unnecessary to do the
> search for those points ?

Subject: Is it possible to convert this for loop into a matrix?

From: Walter Roberson

Date: 27 Dec, 2010 23:54:14

Message: 8 of 14

On 27/12/10 5:27 PM, Haoyang Liu wrote:

> Actually, for a given x and y, V (as a function of another variable, say
> z), has only one maximum.

Another definition would say that it has a maximum in every direction
(if only at the outer bounds) as long as it is not flat or undefined in
those directions. By indicating that it has only one maximum, you are
implicitly asking for a global maximum, which is contrary to the earlier
indication that you were looking for local maxima.

 > But the position (w.r.t. z) and value of the
> maximum is different depending on the x,y pair.

Is the differential available? Is it analytically solvable in at least
one of the variables? If it is available but not analytically solvable
in either variable, then a minimizer routine would need to be used to
determine the 2D zero of the differential, which would greatly reduce
the possibility of vectorization.

If no differential is available, again a minimizer routine would need to
be used, this time to find the minimum of the negative of the function;
there would be very little opportunity for vectorization in such a case.

Subject: Is it possible to convert this for loop into a matrix?

From: Haoyang Liu

Date: 29 Dec, 2010 00:12:05

Message: 9 of 14

Sorry, I should've been more clear. The function V, when plotted against z, has at least two maximums, but only one in the range that I'm interested in.

Analytical solution for the derivative is available for dV/dx, dV/dy, and dV/dz. I guess dV/dz=0 is what I want, but I'm not sure how to integrate that into the vectorization solution you provided.

Thanks,

Walter Roberson <roberson@hushmail.com> wrote in message <SG9So.3684$jj5.3468@newsfe03.iad>...
> On 27/12/10 5:27 PM, Haoyang Liu wrote:
>
> > Actually, for a given x and y, V (as a function of another variable, say
> > z), has only one maximum.
>
> Another definition would say that it has a maximum in every direction
> (if only at the outer bounds) as long as it is not flat or undefined in
> those directions. By indicating that it has only one maximum, you are
> implicitly asking for a global maximum, which is contrary to the earlier
> indication that you were looking for local maxima.
>
> > But the position (w.r.t. z) and value of the
> > maximum is different depending on the x,y pair.
>
> Is the differential available? Is it analytically solvable in at least
> one of the variables? If it is available but not analytically solvable
> in either variable, then a minimizer routine would need to be used to
> determine the 2D zero of the differential, which would greatly reduce
> the possibility of vectorization.
>
> If no differential is available, again a minimizer routine would need to
> be used, this time to find the minimum of the negative of the function;
> there would be very little opportunity for vectorization in such a case.

Subject: Is it possible to convert this for loop into a matrix?

From: Haoyang Liu

Date: 29 Dec, 2010 10:15:05

Message: 10 of 14

I tried this without success:

N=150;
a=rand(N,1).*1e-7;
f = @(x,y) bsxfun(@(x1,y1) (x1+y1)./(1./x1 + 1./y1)./phi_small(x1,y1,fminbnd(@(H) -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))), x, y.');
c=f(a,a);

The error message was:

"??? Operands to the || and && operators must be convertible to logical scalar values.

Error in ==> fminbnd at 310
        if ( (fu <= fw) || (w == xf) )"

if I only use the first part of f, (i.e. f = @(x,y) bsxfun(@(x1,y1) (x1+y1)./(1./x1 + 1./y1), x, y.');), it run without problem, and the second part (i.e. phi_small(x1,y1,fminbnd(@(H) -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))) run fine standalone too.

Any idea?

Thanks,



"Haoyang Liu" wrote in message <ifducl$du0$1@fred.mathworks.com>...
> Sorry, I should've been more clear. The function V, when plotted against z, has at least two maximums, but only one in the range that I'm interested in.
>
> Analytical solution for the derivative is available for dV/dx, dV/dy, and dV/dz. I guess dV/dz=0 is what I want, but I'm not sure how to integrate that into the vectorization solution you provided.
>
> Thanks,
>
> Walter Roberson <roberson@hushmail.com> wrote in message <SG9So.3684$jj5.3468@newsfe03.iad>...
> > On 27/12/10 5:27 PM, Haoyang Liu wrote:
> >
> > > Actually, for a given x and y, V (as a function of another variable, say
> > > z), has only one maximum.
> >
> > Another definition would say that it has a maximum in every direction
> > (if only at the outer bounds) as long as it is not flat or undefined in
> > those directions. By indicating that it has only one maximum, you are
> > implicitly asking for a global maximum, which is contrary to the earlier
> > indication that you were looking for local maxima.
> >
> > > But the position (w.r.t. z) and value of the
> > > maximum is different depending on the x,y pair.
> >
> > Is the differential available? Is it analytically solvable in at least
> > one of the variables? If it is available but not analytically solvable
> > in either variable, then a minimizer routine would need to be used to
> > determine the 2D zero of the differential, which would greatly reduce
> > the possibility of vectorization.
> >
> > If no differential is available, again a minimizer routine would need to
> > be used, this time to find the minimum of the negative of the function;
> > there would be very little opportunity for vectorization in such a case.

Subject: Is it possible to convert this for loop into a matrix?

From: Walter Roberson

Date: 29 Dec, 2010 10:23:22

Message: 11 of 14

On 29/12/10 4:15 AM, Haoyang Liu wrote:
> I tried this without success:
>
> N=150;
> a=rand(N,1).*1e-7;
> f = @(x,y) bsxfun(@(x1,y1) (x1+y1)./(1./x1 +
> 1./y1)./phi_small(x1,y1,fminbnd(@(H)
> -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))), x, y.');
> c=f(a,a);
>
> The error message was:
>
> "??? Operands to the || and && operators must be convertible to logical
> scalar values.
>
> Error in ==> fminbnd at 310
> if ( (fu <= fw) || (w == xf) )"
>
> if I only use the first part of f, (i.e. f = @(x,y) bsxfun(@(x1,y1)
> (x1+y1)./(1./x1 + 1./y1), x, y.');), it run without problem, and the
> second part (i.e. phi_small(x1,y1,fminbnd(@(H)
> -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))) run fine
> standalone too.
>
> Any idea?

Yup. Look at the documentation for bsxfun:

"If an M-file function is specified, it must be able to accept either
two column vectors of the same size, or one column vector and one
scalar, and return as output a column vector of the size as the input
values."

fminbnd() is not able to process column vectors. You will have to wrap
the call to it with arrayfun() over x1

Subject: Is it possible to convert this for loop into a matrix?

From: Haoyang Liu

Date: 29 Dec, 2010 20:17:20

Message: 12 of 14

How should I implement this? I have tried:

>> arrayfun(fminbnd(@(H) -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)
??? Undefined function or variable 'x1'.
Error in ==> @(H)-1*phi_small(x1,y1,H)
Error in ==> fminbnd at 212
x= xf; fx = funfcn(x,varargin{:});

>> arrayfun(@(x1,y1) fminbnd(@(H) -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)
??? Undefined function or variable 'x1'.

Thanks a bunch,

Walter Roberson <roberson@hushmail.com> wrote in message <u_DSo.8614$111.6134@newsfe12.iad>...
> On 29/12/10 4:15 AM, Haoyang Liu wrote:
> > I tried this without success:
> >
> > N=150;
> > a=rand(N,1).*1e-7;
> > f = @(x,y) bsxfun(@(x1,y1) (x1+y1)./(1./x1 +
> > 1./y1)./phi_small(x1,y1,fminbnd(@(H)
> > -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))), x, y.');
> > c=f(a,a);
> >
> > The error message was:
> >
> > "??? Operands to the || and && operators must be convertible to logical
> > scalar values.
> >
> > Error in ==> fminbnd at 310
> > if ( (fu <= fw) || (w == xf) )"
> >
> > if I only use the first part of f, (i.e. f = @(x,y) bsxfun(@(x1,y1)
> > (x1+y1)./(1./x1 + 1./y1), x, y.');), it run without problem, and the
> > second part (i.e. phi_small(x1,y1,fminbnd(@(H)
> > -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))) run fine
> > standalone too.
> >
> > Any idea?
>
> Yup. Look at the documentation for bsxfun:
>
> "If an M-file function is specified, it must be able to accept either
> two column vectors of the same size, or one column vector and one
> scalar, and return as output a column vector of the size as the input
> values."
>
> fminbnd() is not able to process column vectors. You will have to wrap
> the call to it with arrayfun() over x1

Subject: Is it possible to convert this for loop into a matrix?

From: Haoyang Liu

Date: 29 Dec, 2010 21:03:06

Message: 13 of 14

I realized why I was getting that error, as I was running that command in the command window... my mistake.

But I now have this problem:

if I have this function in place of my previous f,

f = @(x,y) bsxfun(@(x1,y1) (x1+y1)./(1./x1 + 1./y1)./phi_small(x1,y1,arrayfun(@(x2,y2) fminbnd(@(H) -1*phi_small(x2,y2,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)), x, y.');

I get this error:

??? Error using ==> arrayfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 150 in dimension 1. Input #3 has size 1.

Error in ==> @(x1,y1)(x1+y1)./(1./x1+1./y1)./phi_small(x1,y1,arrayfun(@(x2,y2)fminbnd(@(H)-1*phi_small(x2,y2,H),0,1e-8,optimset('TolX',1e-10)),x1,y1))

Error in ==> @(x,y)bsxfun(@(x1,y1)(x1+y1)./(1./x1+1./y1)./phi_small(x1,y1,arrayfun(@(x2,y2)fminbnd(@(H)-1*phi_small(x2,y2,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)),x,y.')

Error in ==> inlinefunction at 28
c=f(a,a);

So the phi_small isn't giving me the 150x150 matrix I expected...




"Haoyang Liu" wrote in message <ifg50g$2v5$1@fred.mathworks.com>...
> How should I implement this? I have tried:
>
> >> arrayfun(fminbnd(@(H) -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)
> ??? Undefined function or variable 'x1'.
> Error in ==> @(H)-1*phi_small(x1,y1,H)
> Error in ==> fminbnd at 212
> x= xf; fx = funfcn(x,varargin{:});
>
> >> arrayfun(@(x1,y1) fminbnd(@(H) -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)
> ??? Undefined function or variable 'x1'.
>
> Thanks a bunch,
>
> Walter Roberson <roberson@hushmail.com> wrote in message <u_DSo.8614$111.6134@newsfe12.iad>...
> > On 29/12/10 4:15 AM, Haoyang Liu wrote:
> > > I tried this without success:
> > >
> > > N=150;
> > > a=rand(N,1).*1e-7;
> > > f = @(x,y) bsxfun(@(x1,y1) (x1+y1)./(1./x1 +
> > > 1./y1)./phi_small(x1,y1,fminbnd(@(H)
> > > -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))), x, y.');
> > > c=f(a,a);
> > >
> > > The error message was:
> > >
> > > "??? Operands to the || and && operators must be convertible to logical
> > > scalar values.
> > >
> > > Error in ==> fminbnd at 310
> > > if ( (fu <= fw) || (w == xf) )"
> > >
> > > if I only use the first part of f, (i.e. f = @(x,y) bsxfun(@(x1,y1)
> > > (x1+y1)./(1./x1 + 1./y1), x, y.');), it run without problem, and the
> > > second part (i.e. phi_small(x1,y1,fminbnd(@(H)
> > > -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))) run fine
> > > standalone too.
> > >
> > > Any idea?
> >
> > Yup. Look at the documentation for bsxfun:
> >
> > "If an M-file function is specified, it must be able to accept either
> > two column vectors of the same size, or one column vector and one
> > scalar, and return as output a column vector of the size as the input
> > values."
> >
> > fminbnd() is not able to process column vectors. You will have to wrap
> > the call to it with arrayfun() over x1

Subject: Is it possible to convert this for loop into a matrix?

From: Haoyang Liu

Date: 29 Dec, 2010 21:27:05

Message: 14 of 14

I got it working by doing this:

f = @(x,y) bsxfun(@(x1,y1) (x1+y1)./(1./x1 + 1./y1), x, y.');
f2 = @(x,y) phi_small(x,y,arrayfun(@(x1,y1) fminbnd(@(H) -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10)),x,y));


c1=f(a,a);
c2=f2(repmat(a,1,length(a)),repmat(a',length(a),1));
c3=c1./c2;

However, c2 is taking a very long time to run... c1 takes 0.003158 sec, c2 takes 30.438425 sec, if N = 150.

Any idea how I can speed up c2?

"Haoyang Liu" wrote in message <ifg7ma$s1s$1@fred.mathworks.com>...
> I realized why I was getting that error, as I was running that command in the command window... my mistake.
>
> But I now have this problem:
>
> if I have this function in place of my previous f,
>
> f = @(x,y) bsxfun(@(x1,y1) (x1+y1)./(1./x1 + 1./y1)./phi_small(x1,y1,arrayfun(@(x2,y2) fminbnd(@(H) -1*phi_small(x2,y2,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)), x, y.');
>
> I get this error:
>
> ??? Error using ==> arrayfun
> All of the input arguments must be of the same size and shape.
> Previous inputs had size 150 in dimension 1. Input #3 has size 1.
>
> Error in ==> @(x1,y1)(x1+y1)./(1./x1+1./y1)./phi_small(x1,y1,arrayfun(@(x2,y2)fminbnd(@(H)-1*phi_small(x2,y2,H),0,1e-8,optimset('TolX',1e-10)),x1,y1))
>
> Error in ==> @(x,y)bsxfun(@(x1,y1)(x1+y1)./(1./x1+1./y1)./phi_small(x1,y1,arrayfun(@(x2,y2)fminbnd(@(H)-1*phi_small(x2,y2,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)),x,y.')
>
> Error in ==> inlinefunction at 28
> c=f(a,a);
>
> So the phi_small isn't giving me the 150x150 matrix I expected...
>
>
>
>
> "Haoyang Liu" wrote in message <ifg50g$2v5$1@fred.mathworks.com>...
> > How should I implement this? I have tried:
> >
> > >> arrayfun(fminbnd(@(H) -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)
> > ??? Undefined function or variable 'x1'.
> > Error in ==> @(H)-1*phi_small(x1,y1,H)
> > Error in ==> fminbnd at 212
> > x= xf; fx = funfcn(x,varargin{:});
> >
> > >> arrayfun(@(x1,y1) fminbnd(@(H) -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10)),x1,y1)
> > ??? Undefined function or variable 'x1'.
> >
> > Thanks a bunch,
> >
> > Walter Roberson <roberson@hushmail.com> wrote in message <u_DSo.8614$111.6134@newsfe12.iad>...
> > > On 29/12/10 4:15 AM, Haoyang Liu wrote:
> > > > I tried this without success:
> > > >
> > > > N=150;
> > > > a=rand(N,1).*1e-7;
> > > > f = @(x,y) bsxfun(@(x1,y1) (x1+y1)./(1./x1 +
> > > > 1./y1)./phi_small(x1,y1,fminbnd(@(H)
> > > > -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))), x, y.');
> > > > c=f(a,a);
> > > >
> > > > The error message was:
> > > >
> > > > "??? Operands to the || and && operators must be convertible to logical
> > > > scalar values.
> > > >
> > > > Error in ==> fminbnd at 310
> > > > if ( (fu <= fw) || (w == xf) )"
> > > >
> > > > if I only use the first part of f, (i.e. f = @(x,y) bsxfun(@(x1,y1)
> > > > (x1+y1)./(1./x1 + 1./y1), x, y.');), it run without problem, and the
> > > > second part (i.e. phi_small(x1,y1,fminbnd(@(H)
> > > > -1*phi_small(x1,y1,H),0,1e-8,optimset('TolX',1e-10))) run fine
> > > > standalone too.
> > > >
> > > > Any idea?
> > >
> > > Yup. Look at the documentation for bsxfun:
> > >
> > > "If an M-file function is specified, it must be able to accept either
> > > two column vectors of the same size, or one column vector and one
> > > scalar, and return as output a column vector of the size as the input
> > > values."
> > >
> > > fminbnd() is not able to process column vectors. You will have to wrap
> > > the call to it with arrayfun() over x1

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us