Find each element that is less than some element to its right

后端 未结 6 1712
滥情空心
滥情空心 2021-01-12 03:05

I need to find elements of a vector that are less than one of more elements that come after it. It\'s easy to do in a loop:

x = some_vector_values;
for m = 1         


        
6条回答
  •  无人及你
    2021-01-12 03:59

    If you would like to find elements that are less than some element to its right, you can do this also:

    x = some_values'; % x should be a column vector to use this
    h = hankel(x);
    m = max(h,[],2);
    f = find(x

    The hankel matrix will show the elements to the right as it goes down the rows.

    Then you can use the indices or true/false to iterate through a for loop and perform some operations. Here is an example:

    x =
    
         9
         8
        16
        16
         4
        10
         9
        13
        15
         1
    
    >> h = hankel(x)
    
    h =
    
         9     8    16    16     4    10     9    13    15     1
         8    16    16     4    10     9    13    15     1     0
        16    16     4    10     9    13    15     1     0     0
        16     4    10     9    13    15     1     0     0     0
         4    10     9    13    15     1     0     0     0     0
        10     9    13    15     1     0     0     0     0     0
         9    13    15     1     0     0     0     0     0     0
        13    15     1     0     0     0     0     0     0     0
        15     1     0     0     0     0     0     0     0     0
         1     0     0     0     0     0     0     0     0     0
    
    >> m = max(h,[],2)
    
    m =
    
        16
        16
        16
        16
        15
        15
        15
        15
        15
         1
    
    >> f = find(a

提交回复
热议问题