Matlab:判断值是否在nx2矩阵

时间:2016-11-28 12:22:48

标签: matlab matrix

我正在编写一个Matlab工具,某些流程必须自动完成。

我正在运行for循环,需要做出一些决定。这是我的一段代码:

    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

此处,DecisionMatrix是可变大小的矩阵,总是nx2。我想要做的是确定Position的值何时在DecisionMatrix的任何行的条目之间。当DecisionMatrix是常数矩阵时(如上所示),这应该很容易。但是,此矩阵的行数可变。

因此,你会怎么做?

提前致谢!!

2 个答案:

答案 0 :(得分:2)

确定Position(标量)的值何时在DecisionMatrix(2列矩阵)的任何行的条目之间:

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

以上内容提供了 logical结果truefalse)。如果您需要知道满足条件的行索引

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

答案 1 :(得分:1)

您可以通过引入另一个循环来修复您的代码,并在找到所需的行时将其输出。

    - (void)viewDidLoad
    {
        [super viewDidLoad];


         [_BtnDate addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
         _BtnDate.titleLabel.font = [UIFont systemFontOfSize:17];
        [[_BtnDate layer] setBorderWidth:1];
        [[_BtnDate layer] setCornerRadius:5];
        [[_BtnDate layer] setBorderColor:[UIColor lightGrayColor].CGColor];
        [_BtnDate setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.view addSubview:_BtnDate];


    }


    -(void) showDateView{

        viewBack2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
        [viewBack2 setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
        [self.view addSubview:viewBack2];


        dateView = [[UIView alloc] initWithFrame:CGRectMake((viewBack2.frame.size.width-300)/2, (viewBack2.frame.size.height-350)/2, 300, 350)];
        [self.view addSubview:dateView];

        UIImageView *imageBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-popup"]];
        [imageBackground setFrame:CGRectMake(0, 0, 300, 350)];
        [dateView addSubview:imageBackground];
        [imageBackground release];

        UIButton *BtnDate = [UIButton buttonWithType:UIButtonTypeCustom];
        [BtnDate setBackgroundImage:[UIImage imageNamed:@"okay.png"] forState:UIControlStateNormal];
        [BtnDate addTarget:self action:@selector(btnDateAction:) forControlEvents:UIControlEventTouchUpInside];
        [BtnDate setFrame:CGRectMake(80, 300, 140, 36)];
        [dateView addSubview:BtnDate];


        datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(25,25, 250, 250)];
        datePicker.datePickerMode = UIDatePickerModeDate;

        datePicker.hidden = NO;
        datePicker.date = [NSDate date];
        [datePicker addTarget:self
                       action:@selector(changeDateInLabel:)
             forControlEvents:UIControlEventValueChanged];
        [dateView addSubview:datePicker];



    }


    - (void)changeDateInLabel:(id)sender{

        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd"];
        if (_BtnDate.selected==true) {
            [_BtnDate setTitle:[NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]] forState:UIControlStateNormal];
        }

        [df release];
    }


    -(IBAction)btnDateAction:(id)sender{

        if (_BtnDate.selected==true) {
            _BtnDate.selected=false;
        }

        [dateView removeFromSuperview];
        [viewBack2 removeFromSuperview];


    }



    - (void) buttonPressed:(UIButton*)sender {

        if (sender == _BtnDate) {

            [self showDateView];
            _BtnDate.selected=true;
        }


    }


    - (IBAction)btnAddForm:(id)sender {


    // I WANT TO CHECK _myText if is not null or > 0 must be clear _BtnDate.titleLabel.text  dont do this !


     if ([NSNumber numberWithInt:[_myText.text intValue]] > 0 )  {

            _BtnDate.titleLabel.text = @"";


      }
}


    - (void)dealloc {
        [_myText release];
        [_BtnDate release];
        [super dealloc];
    }
    - (void)viewDidUnload {
        [self setmyText:nil];
        [self setBtnDate:nil];
        [super viewDidUnload];
    }