Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Analytic expression to find the range of non-negative numbers in array

  1. Nov 12, 2013 #1

    I have an array of length M. Some of the indices are non-negative. I need to derive a function/analytic expression (preferably linear or convex) that finds the span of indices for which the indices are non-negative.

    Example: Let p denote the desired vector.

    Let, p = [0 0 0.3 2.6 0 0 1.1 0 1.8 0]

    There are 10 elements in the array. Non-negative numbers start from index 3 (0.3) and end at index 9 (1.8). Range/span of non-negative numbers = 9-3+1 = 7.

    I need a linear/convex function/analytic expression that can find this number.

    Previous work:

    I have already derived a similar function if the array only contains binary numbers.

    Example: let, x = [0 0 1 1 0 0 1 0 1 0].

    Now, let M denote the set of indices of x, i.e., M = {1,2,3,...,10}. Assume that x_m denotes the
    m-th entry of the array x. For example, x_2 = 0, x_3 = 1. Here, |M| = 10.

    My derived function (written in Latex format),
    f(x) = max_{m \in M} (m * x_m) - min_{m \in M} (m * x_m + |M| (1 - x_m)) + 1

    = max (1 * x_1, 2 * x_2, ..) - min (1 * x_1 + 10 * (1 - x_1), 2 * x_2 + 10 * (1 - x_2), ...) + 1

    = 9 - 3 + 1

    = 7.

    This function is convex and it provides the correct answer. However, it only works for arrays that consist of binary numbers.

    I need to find a linear/convex function/analytic expressions which work for arrays that consist of any non-negative number. This function will go into a constraint of my optimization problem. Hence, linearity/convexity is required. However, even a non-linear/non-convex function may help me, too.

    Any ideas? Thanks a lot for reading this message. Let me know if I should post it in a different sub-forum of the math forum.


  2. jcsd
  3. Nov 13, 2013 #2


    Staff: Mentor

    No. Based on what you have said below, all indexes are positive (1, 2, 3, etc.). Also, no elements of your array are negative, but some of them are zero. It might be a language translation problem. Nonnegative means "not negative" and includes numbers that are either zero or positive.
    Based on your example, what you're looking for is a function that finds the number of elements that are positive.
    All elements in your array are nonnegative. Some elements are zero and the rest are positive.

    Pretty much any programming language can be used to write a simple function that returns the number of positive elements in an array.

    In C, the code would look like this:

    Code (Text):

    int positiveElements(float arr[], int count)
      int i;
      int numPos = 0;

      if (count != 0)
        for (int i = 0; i < count; i++)
          if (arr[i] > 0) numPos++;
      return numPos;
  4. Nov 13, 2013 #3
    Hello Mark44,

    Thanks for your reply. I appreciate your effort.

    Actually, I probably confused readers by mentioning "non-negative indices". Ideally, I want to find the SPAN of indices that correspond to the "positive elements" in an array. Note that, it is not equal to the number of "positive elements" in an array.

    For example, let p = [0 0 0.3 2.6 0 0 1.1 0 1.8 0].

    Here, there are 4 positive elements in the array (0.3, 2.6, 1.1, 1.8). However, I want to find the span of indices for which the elements are positive, i.e., the difference between the rightmost and leftmost index for which the element is positive.

    In this case, the leftmost index is 3 (corresponding value of the element is 0.3). The rightmost index is 9 (corresponding value of the element is 1.8). Hence, the span is, 9-3+1 = 7.

    I know how to find this "span" using a C code. I just need to find an analytic expression that can find this expression when the vector is given.

    I have already derived an analytic expression when the elements are binary (shown in my original post). I need to derive an expression where the elements of the vectors are real numbers.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook