How to extract submatrix from the Main matrix???

11 views (last 30 days)
HI everyone!!
I have a matrix 'Z' with values of '1' and '0'. Mat file is attached.
Now i'd like to extract sub matrices from 'Z' which consists of '1' with their corresponding index values .
example:
Z=size(15,10).
From'Z', i should get output like these three matrices in a loop.
a=Z(3:6,2:6);
b=Z(7:9,6:7);
c=Z(8:15,2);
thanks in advance!

Accepted Answer

Guillaume
Guillaume on 31 Jul 2019
If you have the image processing toolbox:
subnatrices = struct2cell(regionprops(logical(Z), 'Image'))
  3 Comments
Guillaume
Guillaume on 31 Jul 2019
Have a look at the documentation of regionprops. It can output all sorts of properties. In particular the 'PixelIdxList' (if you want linear indices) or 'PixelList' (if you want 2D indices) will give the indices of the 1s in each submatrix. Or perhaps you want the 'BoundingBox'
submatrices = regionprops(logical(Z), {'Image', 'PixelList', 'PixelIdxList'});
will get you a structure where
  • submatrices(i).Image is the ith submatrix
  • submatrices(i).PixelList is the 2D list of indices of 1s in the submatrix,
  • etc.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!