How can I find consecutive integers in a vector?
3 次查看(过去 30 天)
显示 更早的评论
Hi everybody. I'm working with a binary image, and for each row and column I have to count how many pixels are 'on' (pixel's value is 1). This information is stored in 2 vectors: one storing the rows value and the second one for columns. Now I've made 2 binary vectors indicating wether the value of the previous vectors is higher than a given Treshold value (in this case value is 1), or not (value is 0).
Now I have to count how many rows/columns consecutively have a value higher than the threshold, so it's about counting how many consecutively 1s are stored in the binary vectors, but I don't know how to do that.
How can i do that, does matlab have a function for it?
0 个评论
回答(2 个)
Image Analyst
2017-2-4
What's wrong with the sum() function?
countPerColumn = sum(binaryImage, 1);
countPerRow = sum(binaryImage, 2); % Going across columns within a row.
rowCountsAboveThreshold = sum(countPerRow > threshold);
colCountsAboveThreshold = sum(countPerColumn > threshold);
Of course if your threshold is only 1, then you can use the any() function.
0 个评论
Walter Roberson
2017-2-4
RunLength encoding from the File Exchange can tell you how many 1s and 0's in a row in each case.
Another strategy is:
row = reshape(YourVector, 1, []); %need it to be a row for the below
start_pos = strfind([0 row 0], [0 1]);
end_pos = strfind([0 row 0], [1 0]) - 1;
Now start_pos and end_pos are in pairs and indicate the start and end positions of runs. The difference between the two, plus 1, gives the length of the run.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!