MATLAB Answers

Find ones blocks in upper triangular matrix

8 views (last 30 days)
Na
Na on 19 Mar 2020
Edited: Na on 12 Apr 2020
I have A
A=[0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 1 0 1 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0 0 0;
0 0 0 0 1 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 0 0 1 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0];
I want to find blocks of one in this matrix according to
blocks={[2,3,4,5,6,7],[6,7,8,9,10],[9,10,11,11,12]}

  0 Comments

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 19 Mar 2020
Edited: John D'Errico on 19 Mar 2020
Let me plead for you to change how you are doing things. What you are asking to do is perhaps the most silly form of information storage I have seen in a while. To understand what I mean by that, what block is decribed by the set:
[7 8 9 10 11]
That is, does this describe a 1x4 rectangle, thus with 1 row (row 7) and 4 columns (8:11)? Or perhaps a 2x3 rectangle? Or perhaps a 3x2 rectangle? Or perhaps a 4x1 rectangle?
Do you recognize this is just an information storage idea that is just begging for a future head pounding bug, where you then need to disambiguate which of those many possible boxes was intended?
A far better solution is to just indicate the box by 4 numbers, ALWAYS, thus the first and last row and the first and last column in said box.
As far as how to find those bounding boxes, this should not be that difficult. For any 1 in the array, if another element is within 1 pixel, you would necessarily merge the two corresponding boxes, though I'm not sure what rules you may have for those mergers, so I must stop here for the moment. In fact, I am pretty confidant that if Image Analyst views this question, his response will be a simple, direct solution based on the IPT. If he does, then use it! :)

  4 Comments

Show 1 older comment
Na
Na on 20 Mar 2020
I appreciate your taking the time to answer my question.
I do not understant first and second element of ans
props.BoundingBox
ans =
4.5000 1.5000 3.0000 3.0000
---------------- -----------------
??? size of the block
John D'Errico
John D'Errico on 20 Mar 2020
Guillaume - you should post that as an answer. I lack the IPT, so I cannot do so.
To respond to Na, I think this is simply a problem of definition. Where does the edge of a bounding box live? Is it midway between pixels?
Guillaume
Guillaume on 20 Mar 2020
Indeed, matlab considers the edge of the bounding box to be half-way between two pixels, so coordinates are always 0.5 less than the pixel index which is at the edge. That's why you've got + 0.5 in the code I wrote.
@John, the reason I didn't post it as an answer is because I feel your point about the data storage is a lot more important than the method by which the result is obtained.
@Na, Note that you may be more interested in some other properties returned by regionprops such as Image (which would be each submatrix) or PixelList (the coordinates of each 1 within each block).

Sign in to comment.

More Answers (0)

Sign in to answer this question.