help for the algorithm
조회 수: 1 (최근 30일)
이전 댓글 표시
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...
댓글 수: 0
답변 (1개)
Image Analyst
2013년 10월 18일
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
Image Analyst
2013년 10월 18일
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
2013년 10월 18일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!