tags:

views:

41

answers:

2

I have a matrix say:

Q = [05 11 12 16 25;
     17 18 02 07 10;
     04 23 20 03 01;
     24 21 19 14 09;
     06 22 08 13 15]

I would like to list out all the possible 3x3 matrices. Some examples are:

11 12 16;
18  2  7;
23 20  3

and

 5 11 12;
17 18  2;
 4 23 20;

etc.. Basically all the possible 3-by-3 matrices. How do I do it? I must use for loop?

+3  A: 

If you have the Image Processing Toolbox, you can use the function IM2COL:

subMats = im2col(Q,[3 3]);

Each column of subMats contains the elements of a 3-by-3 matrix extracted from Q. Each of these columns can be reshaped into a 3-by-3 matrix as follows:

Q1 = reshape(subMats(:,1),[3 3]);  %# Reshape column 1 into a 3-by-3 matrix
gnovice
for the second part, you can combine all 3-by-3 matrices in the third dimension: `Z = reshape(permute(subMats,[1 3 2]), 3,3,[])`, so that you can access each using `Z(:,:,k)`
Amro
A: 

I'm guessing this is homework (if not, please forgive me), so here are some hints.

  • Draw out the structure of your 5x5 matrix.
  • Start in the upper left and draw a 3x3 submatrix within that 5x5. What are the elements covered by that matrix?
  • Go to the upper right. What elements are covered there?
  • Now go to the lower left. What about there?

Do you see how to cover the whole thing?

John at CashCommons

related questions