Getting all submatrices
Asked Answered
A

1

6

I have got an N×M matrix m like:

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16

I want to get all submatrices of size P×Q (P,Q are odd) w/o employing a for-loop.

The result s should be a P×Q×((N-P+1)·(M-Q+1)) matrix.

E.g. if P=Q=3:

s(:,:,1) = [1 2 3;  5  6  7;  9 10 11]
s(:,:,2) = [2 3 4;  6  7  8; 10 11 12]
s(:,:,3) = [5 6 7;  9 10 11; 13 14 15]
s(:,:,4) = [6 7 8; 10 11 12; 14 15 16]
Anastomose answered 4/12, 2012 at 21:15 Comment(3)
Why are there multiples? Eg. 5 6 7 appears twice.Additional
@Additional sorry, my example was confusing. Is it easier to understand now?Anastomose
No, I still don't quite understand: Why would you want to have duplicate entries? Just so that you can fill up your array? Also, have you had a look at my solution?Additional
M
6

im2col can help you out here:

m =
     1     2     3     4
     5     6     7     8
     9    10    11    12
    13    14    15    16

>> P = 3; Q = 3;
>> columnized = im2col(m,[P Q],'sliding');
>> nMatrices = size(columnized,2);
>> s = reshape(columnized, [P Q nMatrices])

s(:,:,1) =
     1     2     3
     5     6     7
     9    10    11
s(:,:,2) =
     5     6     7
     9    10    11
    13    14    15
s(:,:,3) =
     2     3     4
     6     7     8
    10    11    12
s(:,:,4) =
     6     7     8
    10    11    12
    14    15    16

im2col with the 'sliding' option finds all the overlapping submatrices and returns each as a (P·Q)-element column vector in columnized. To turn these back into matrices, we reshape this (P·Q)×((N-P+1)·(M-Q+1)) matrix into a P×Q×((N-P+1)·(M-Q+1)) one.

Merger answered 5/12, 2012 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.