help for the algorithm

1 view (last 30 days)
husot
husot on 18 Oct 2013
Commented: Image Analyst on 18 Oct 2013
I have a matrix shown below. And I scan all ( :,1) within a "for" loop. What I want to do is, if the loop finds 3 same number in succession (for example 8 in (7,1), 8 in (8,1) and 8 in (9,1) ) take their datas on their 4th column and put these 3 datas to the function I made. If the loop finds 4 same number in succesion to the same thing again. There is no problem until here. I have 2 "if" loops in my "for" loop. One for 3-group and one for 4-group.
The problem is when the loop finds 4 same number in succession (like 12.0000 in the first column) and do the rest of things for the function, it continues with the next row as usual. And then on the next row (value= 16.0000) it sees that on the last 3 rows, there are 3 same numbers again. It takes the the same datas again with in 3-group and try to process it. I don't want that. I don't want to access to 3 same number in 4th-group same number. I wrote that too long but it looks very simple to correct it. Does anyone know how to fix it?
Thanks...

Answers (1)

Image Analyst
Image Analyst on 18 Oct 2013
This is very easy if you have the Image Processing Toolbox, which has regionprops() that makes it easy to find stretches of 3 or more elements in your column. Do you have that toolbox? If so, use diff to get stretches of 0 where there is no change in the number, then call regionprops to find where the stretches of 0 are 2 or 3 in length (meaning 3 or 4 or the same number next to each other).
  2 Comments
Image Analyst
Image Analyst on 18 Oct 2013
husot's "Answer" moved here since it's a comment to my response, not an answer by itself:
Thanks for the answer but unfortunately I don't have the image processing toolbox. Is there another way to overcome this problem?
Image Analyst
Image Analyst on 18 Oct 2013
People have posted connected components labeling routines here that work for 1D vectors. They are kind of complicated and I don't have a link for you so maybe someone will do it again. Otherwise, search the web for algorithms or code to do "Connected components labeling" or just use diff() and then do it the brute force way of going down and counting zeros that are adjacent with a for loop.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!