MATLAB: Find ones blocks in upper triangular matrix

find ones blocks in upper triangular matrix

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]}

Best Answer

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! :)