Balraj Boyal
Balraj Boyal

Reputation: 155

Matlab: deciding whether a value is between the column entries of a nx2 matrix

I am writing a Matlab Tool and certain processes have to be automated.

I am running a for loop, in which some decisions need to be made. Here is a piece of my code:

    DecisionMatrix = [0.2 0.4; 0.5 0.7];

        Beta =0:pi/20:pi;
        Span_Loc = 0.5*(1-cos(Beta))';  

        for i=1:length(Span_Loc)
          Position = Span(i)

    % Check Clean of High Lift
    if Position >=  DecisionMatrix(1,1) && Position <=  DecisionMatrix(1,2)

% HighLift run  code here

    elseif Position >=  DecisionMatrix(2,1) && Position <=    DecisionMatrix(2,2)



    else

% Clean run code here
    end

        end

Herein, DecisionMatrix is a variable size matrix which is nx2 always. What I want to do is to determine when the value of Position is between the entries of any row of DecisionMatrix. This should be easy when DecisionMatrix is a constant matrix (as shown above). However, this matrix has a variable number of rows.

Hence, how would you do this?

Thanks in advance!!

Upvotes: 0

Views: 38

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112669

To determine when the value of Position (scalar) is between the entries of any row of DecisionMatrix (2-column matrix):

result = any(Position>=DecisionMatrix(:,1) & Position<=DecisionMatrix(:,2));

The above gives a logical result (true or false). If you need to know the indices of the rows that fulfill the condition:

result = find(Position>=DecisionMatrix(:,1) & Position<=DecisionMatrix(:,2));

Upvotes: 2

Sardar Usama
Sardar Usama

Reputation: 19689

You can fix your code by introducing another loop and coming out of it when you find the required row.

DecisionMatrix = [0.2 0.4; 0.5 0.7];
Beta =0:pi/20:pi;
Span_Loc = 0.5*(1-cos(Beta))';

for p=1:length(Span_Loc)
    Position = Span(p);
    for q=1:n
        if Position >=  DecisionMatrix(q,1) && Position <=  DecisionMatrix(q,2)
            %do what you want when the condition is true
            break
        end
    end
end

Upvotes: 1

Related Questions