Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Ensuring Positive Values – Part 1

Do you sometimes need to be sure an array has only bounded, positive values? At least one customer asked about this recently and noted that it could be quite involved. Depending on the duration for which you need to ensure this, there is more than one way to attack this.

Contents

Possible Scenarios

Here are the two main scenarios I can think of.

  • Check that values are all in range only at certain points in a calculation.
  • Ensure that values are always in range throughout their lifetime.
  • I would be tempted to attack these scenarios in different ways. This week, I'll cover the first scenario.

    Point Checks

    If I were only going to check once in a while, I might write some code using some simple MATLAB functions and check details methodically.

    Method 1 - Manual Checking

    The first method would try to test every assumption I can think of, perhaps like this:

        ispositive = ( ~isnumeric(x) ...
                     | ~all(isfinite(x(:))) ...
                     | ~isreal(x) ...
                     | ~(any(x(:) <= 0)));
        if ~ispositive
           doSomething(perhaps, throw, anError);
        end

    There are some possible gotchas with this. There is a long of list of is* functions. Which ones do we need to consider? For example, old-style graphics handles are stored as doubles so they could confuse the results.

    Method 2 - Make an Assertion

    I can embed these checks into an assertion at places in my code where I want to be sure the data is correct. To not have to recreate the logic everytime, I can construct a function handle (anonymous in this case) and use it when I make each assertion.

        fhispositive = @(x) ( ~isnumeric(x) ...
                     | ~all(isfinite(x(:))) ...
                     | ~isreal(x) ...
                     | ~(any(x(:) <= 0)));
        assert(fhispositive(x), ...
              'myapp:checkData:notPos', ...
              'Data not all positive.');

    I am now free to reuse the function handle with assert in strategic places throughout the code, not just at the one place where I typed the original code.

    Note: you can do this as well by creating a separate function (in a separate file or a subfunction) to do the checking. Additionally, assert is only one possible behavior for the case where the data don't comply with the assumptions. You could instead set the data to a default value, for example.

    Method 3 - Validate the Array

    In a MATLAB release R2007b, you'll find the function validateattributes (as well as validatestring). Using validateattributes, you can more compactly check data properties. You will get an error if the data doesn't match the requirements.

        validateattributes(x,{'numeric'},...
                     {'finite','positive'});

    Interesting Functions to Consider

    Here's a list of interesting functions to consider helping you with such checks including the is* functions mentioned above.

    Stayed Tuned...

    Stay tuned for a demo of a solution to the second scenario where you always want to ensure that the data is in a valid state. In the meantime, I'd love to hear if you have requirements or constraints where controlling the values in an array are important. Let me know here.




    Published with MATLAB® 7.9


    • print

    Comments

    To leave a comment, please click here to sign in to your MathWorks Account or create a new one.