how to find locations of consecutive blocks of ones
20 views (last 30 days)
Show older comments
dear all,
I have this matrix and I want to now how to find the consecutive blocks of ones in row 2 and 3, then the blocks in row 4 and 5 such that I specify their locations in their rows that block 5 is flow block 4 immediately in the next row, I think I need to use the index with some condition, but how?
A = [1 0 1 0 1
0 1 0 1 1
0 0 0 1 1
1 1 0 0 1
0 0 1 1 0
0 1 1 1 0];
I wrote this and I can't continue
for i=1:size(A,1)
tra=diff([0 A(i,:) 0]);
ss=find(tra==1);
u=find(tra==-1)-find(tra==1);
u1=[u1 u];
clear max
x=max(u1)
[I,J]=find(x)
end
by the way max command not work in my matlab R2013a.
thanks in advance to any help.
regards,
Imola
2 Comments
dpb
on 28 Mar 2015
_"...by the way max command not work in my matlab R2013a."
max=max(u1)
You just aliased the builtin max by turning it into a variable max. Don't do that.
clear max
will fix the problem once you then also rename the variable to something besides max
Accepted Answer
Image Analyst
on 28 Mar 2015
I don't really know how you're defining the words "find", "get", "recognize" and "better". I think what you want can be done with the function bwlabel() in the Image Processing Toolbox:
[labeledA, numRegions] = bwlabel(A, 4);
This will find regions that are "connected" regardless of how many rows they cover. Each separate region will have it's own label (ID number). You can also get the exact row,column location of the 1's in each region if you want
regionsNumberThatYouWant = 2; % Whatever region number you want to find coordinates of.
[rows, columns] = find(labeledA == regionsNumberThatYouWant);
If you want to operate on just certain rows instead of the whole matrix, then just pass it the rows you want:
[labeledA2, numRegions2] = bwlabel(A(2:3, :), 4);
[labeledA2, numRegions4] = bwlabel(A(4:5, :), 4);
I don't know what "better" means but there's a possibility that you mean that the connected region of 1's has the most number of 1's in it. If you want to find the region that has the greatest number of 1's in it, regardless of how many rows the region snakes across, and how many 1's are in it, then it's just a few lines of code more. Let me know if you need that.
There are also a whole bunch of other things you can determine about each region if you use the regionprops() function.
4 Comments
More Answers (1)
Konstantinos Sofos
on 28 Mar 2015
Edited: Konstantinos Sofos
on 28 Mar 2015
Hi Imola,
If i understood well you want to build a vector which contains the maximum number of consecutive 1s values of each row of your matrix. More or less you could do this with a for loop but i want to give you as an answer a more elegant way and in my opinion is the most appropriate when you have to do with array functions.
The idea is to write a function e.g. myfunc which will be applied in each matrix row and give you back your desired result. So save the following function:
function out = myfunc(x)
out = max( diff( [0 (find( ~ (x > 0) ) ) numel(x) + 1] ) - 1);
end
Now you want to apply this function to each row of your matrix A. Have a look in the documentation of the arrayfun. Type now the following into your command and try to understand each step
applyToGivenRow = @(func, matrix) @(row) func(matrix(row, :))
applyToRows = @(func, matrix) arrayfun(applyToGivenRow(func, matrix), 1:size(matrix,1))'
A = [1 0 1 0 1
0 1 0 1 1
0 0 0 1 1
1 1 0 0 1
0 0 1 1 0
0 1 1 1 0];
f = @myfunc;
V=applyToRows(f, A);
V =
1
2
2
2
2
3
Regards
0 Comments
See Also
Categories
Find more on Logical 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!