how to find locations of consecutive blocks of ones

26 ビュー (過去 30 日間)
imola
imola 2015 年 3 月 28 日
編集済み: imola 2015 年 3 月 31 日
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 件のコメント
dpb
dpb 2015 年 3 月 28 日
_"...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 2015 年 3 月 28 日
Thanks dpb for the advice.
regards

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2015 年 3 月 28 日
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 件のコメント
imola
imola 2015 年 3 月 30 日
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 2015 年 3 月 31 日
編集済み: imola 2015 年 3 月 31 日
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

サインインしてコメントする。

その他の回答 (1 件)

Konstantinos Sofos
Konstantinos Sofos 2015 年 3 月 28 日
編集済み: Konstantinos Sofos 2015 年 3 月 28 日
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

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by