how to find locations of consecutive blocks of ones

26 vues (au cours des 30 derniers jours)
imola
imola le 28 Mar 2015
Modifié(e) : imola le 31 Mar 2015
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 commentaires
dpb
dpb le 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
imola
imola le 28 Mar 2015
Thanks dpb for the advice.
regards

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 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 commentaires
imola
imola le 30 Mar 2015
Dear,
what about my code because sofos's code doesn't give all the blocks, while mine is give all the blocks in every row. you mean trying some thing like this? for every row.
u(j) = find(g == -1) - find(g == 1)
I tried some think like this but how to start with the region and end depending on the index give me problem.
regards
imola
imola le 31 Mar 2015
Modifié(e) : imola le 31 Mar 2015
Dear Image Analyst,
I did it thanks very much. I have one question, this function (bwlabel) work in my laptop (R2013a) but not in my laptop with matlab (R2012b), is it possible that the function is not their?.
regards

Connectez-vous pour commenter.

Plus de réponses (1)

Konstantinos Sofos
Konstantinos Sofos le 28 Mar 2015
Modifié(e) : Konstantinos Sofos le 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

Catégories

En savoir plus sur Historical Contests dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by