How to extract submatrix from the Main matrix???

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

If you have the image processing toolbox:
subnatrices = struct2cell(regionprops(logical(Z), 'Image'))

3 Comments

Thank you Guillaume for your answer.
Its working Good. But I also need to get their corresponding indices values.
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

Asked:

on 31 Jul 2019

Commented:

on 31 Jul 2019

Community Treasure Hunt

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

Start Hunting!