简体   繁体   中英

if a<x<b in matlab

I need any help for Matlab's thinking method.Ithink I can explaine my problem with a simple example better. Let's say that I have a characteristic function x=y+x0, x0's are may starting values.Then I want to define my function in a grid.Then I define a finer grid and I want to ask him if he knows where an arbitrary (x*,y*) is.To determine it mathematically I should ask where the corresponding starting point (x0*) is. If this startig point stay between x(i,1)

    clear
    %%%%%%%%%%&First grid%%%%%%%%%%%%%%%%%%%%
    x0=linspace(0,10,6);
    y=linspace(0,5,6);
    for i=1:length(x0)
        for j=1:length(y)
            x(i,j)=y(j)+x0(i);

    %%%%%%%%%%%%%%%%%%%Second grid%%%%%%%%%%%%%%%%%%
    x0fine=linspace(0,10,10);
    yfine=linspace(0,5,10);
    for p=1:length(x0fine)
        for r=1:length(yfine)
            xfine(p,r)=yfine(r)+x0fine(p);
    if (x(i,1)<xfine(p,1)')&(x0fine(p,1)'<x(i+1,1))%%%%I probabliy have my first mistake %here
%             if y(j)<yfine(r)<y(j+1)
%                 xint(i,j)=(x(i,j)+x(i,j+1)+x(i+1,j)+x(i+1,j+1))./4;
%             else 
%                xint(i,j)= x(i,j);
            %end
end
end
end
end

While a < b < c is legal MATLAB syntax, I doubt that it does what you think it does. It does not check that a < b and b < c . What it does is, it checks whether a < b , returning a logical value (maybe an array of logicals) and then, interpreting this logical as 0 or 1 , compares it against c:

>> 2 < 0 < 2

ans =

     1

>> 2 < 0 < 1

ans =

     1

>> 0 < 0 < 1

ans =

     1

First in matlab you should avoid as much as possible to do loops. For instance you can compute x and xfine, with the following code:

x0=linspace(0,10,6);
y=linspace(0,5,6);
x=bsxfun(@plus,x0',y);
x0fine=linspace(0,10,10);
yfine=linspace(0,5,10);
xfine=bsxfun(@plus,x0fine',yfine);

Then given (X*,y*) your want to fine x0*, in your simple example, you can just do: x0*=x*-y*, I think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM