Finding start and end indices of positive curves in data

Novice_Developer

Consider the following example where x is

x = [randi([10 20],1,10) zeros(1,11) randi([10 20],1,10) zeros(1,11) randi([10 20],1,10) zeros(1,12) randi([10 20],1,11) ];

which looks something like this in the plot:

image

Now what I want to find is the starting and ending index of each positive curve in the graph and store them in a matrix. The code I have written below works fine but it isn't what I will call elegant. Does anyone have a better and more efficient way of doing it?

zero_padding = zeros(1,2);
x = [randi([10 20],1,10) zeros(1,11) randi([10 20],1,10) zeros(1,11) randi([10 20],1,10) zeros(1,12) randi([10 20],1,11) ];
x = [x zero_padding]
stp(1) = find(x>0 , 1, 'first')
enp(1) = find(x(stp(1):end)==0 , 1, 'first') - 1
stoffset = enp(1);
i = 2
while(~isempty(find(x(stoffset + 1:end)>0 , 1, 'first')))        
    stp(i) = find(x(stoffset + 1:end)>0 , 1, 'first') + stoffset
    enp(i) = find(x(stp(i) + 1:end) == 0 , 1, 'first') + stp(i) - 1
    stoffset = (enp(i)) + 1;
    i = i + 1;    
end

and the output is:

>> stp =

     1    22    43    65


>> enp =

    10    31    52    75

which is correct.

NOTE: I have already tried using:

startindex = (find(diff(x)>0)) + 1
endindex = (find(diff(x)<0))

which would work just fine if the positive curves were just perfect squares but as you can see in the plots, it has many dips. So it's difficult to use it as diff will also give +1 or -1 at those dips.

Dan

Your second approach was pretty close i.e.

startindex = (find(diff(x)>0)) + 1
endindex = (find(diff(x)<0))

Rather try

posData = [0, x>0] %prepend with zero to catch if the data starts with a positive region
startindex = find(diff(posData) == 1) 
endindex = find(diff(posData) == -1) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R: finding indices of columns in a data.frame

From Dev

Finding EditText Editable getSpans Start and End Index

From Dev

Finding week start and week end date

From Dev

Finding start and end point of sentence in a paragraph StanfordCoreNLP

From Dev

Finding the max integer in an array, with specified start and ending indices in Java, **recursively**

From Dev

Index matrix based on another matrix of start and end indices

From Dev

numpy get slices of array between start and end indices

From Dev

Match data by start and end times

From Dev

Finding start and end date from given year and month in Unix

From Dev

oracle sql - finding entries with dates (start/end column) overlap

From Dev

Laravel: Finding row by xp_start and xp_end

From Dev

Finding the minimum positive value

From Dev

Finding specific positive integer

From Dev

Finding indices of elements in vector

From Dev

Recursively finding indices of a string

From Dev

Finding all indices by ismember

From Dev

Finding all indices by ismember

From Dev

Recursively finding indices of a string

From Dev

Python: finding the intersection point of two gaussian curves

From Dev

Python: finding the intersection point of two gaussian curves

From Dev

Finding number of positive numbers in list

From Dev

Finding number of positive numbers in list

From Dev

Start reading data in query from end (of file)

From Dev

Finding The First Empty Row At The End Of The Column And Pasting Data In It, Excel VB

From Dev

How to find indices for sequential NaNs at the start and end of a1D NumPy array?

From Dev

finding specific indices with pointer array

From Dev

Finding indices of element in array ruby

From Dev

Finding Indices of Unique Elements in R

From Dev

Finding the column indices of submatrices in MATLAB

Related Related

  1. 1

    R: finding indices of columns in a data.frame

  2. 2

    Finding EditText Editable getSpans Start and End Index

  3. 3

    Finding week start and week end date

  4. 4

    Finding start and end point of sentence in a paragraph StanfordCoreNLP

  5. 5

    Finding the max integer in an array, with specified start and ending indices in Java, **recursively**

  6. 6

    Index matrix based on another matrix of start and end indices

  7. 7

    numpy get slices of array between start and end indices

  8. 8

    Match data by start and end times

  9. 9

    Finding start and end date from given year and month in Unix

  10. 10

    oracle sql - finding entries with dates (start/end column) overlap

  11. 11

    Laravel: Finding row by xp_start and xp_end

  12. 12

    Finding the minimum positive value

  13. 13

    Finding specific positive integer

  14. 14

    Finding indices of elements in vector

  15. 15

    Recursively finding indices of a string

  16. 16

    Finding all indices by ismember

  17. 17

    Finding all indices by ismember

  18. 18

    Recursively finding indices of a string

  19. 19

    Python: finding the intersection point of two gaussian curves

  20. 20

    Python: finding the intersection point of two gaussian curves

  21. 21

    Finding number of positive numbers in list

  22. 22

    Finding number of positive numbers in list

  23. 23

    Start reading data in query from end (of file)

  24. 24

    Finding The First Empty Row At The End Of The Column And Pasting Data In It, Excel VB

  25. 25

    How to find indices for sequential NaNs at the start and end of a1D NumPy array?

  26. 26

    finding specific indices with pointer array

  27. 27

    Finding indices of element in array ruby

  28. 28

    Finding Indices of Unique Elements in R

  29. 29

    Finding the column indices of submatrices in MATLAB

HotTag

Archive