For example I have this matrix(3x3) and I want to make a new 2x2 matrix with the max values from all the submatrixes :

  = [
     5 4 6
     3 2 3
     8 8 9

first submatrix:

[5 4
 3 2 ]
 max value=5

second submatrix:

[4 6
 2 3]
     max value=6

third submatrix:

[3 2
 8 8 ]
     max value=8

fourth submatrix:

[2 3
8 9]
max value=9

and I want to get this matrix(2x2) that has all the max values as elements of the previous submatrixes:

 NewA=[5 6
       8 9]

One last thing the only thing you can use are basic things like for loops if statements....(simple solutions,begginer solutions) Also you can't use the max function,you have to write the code for that,and the solution should work for every square matrix

3

There are 3 best solutions below

0
On

Since the sub-matrices are 2x2 you can manually compute the max:

B = max(max(max(A(1:end-1, 1:end-1), A(1:end-1, 2:end)), A(2:end, 1:end-1)), A(2:end, 2:end));

or

B = max(cat(3, A(1:end-1, 1:end-1), A(1:end-1, 2:end), A(2:end, 1:end-1), A(2:end, 2:end)), [], 3);

If A is a square matrix both methods can be written more compact as:

n = size(A, 1);
x = 1:n-1;
y = 2:n;
B = max(max(max(A(x,x), A(x,y)), A(y,x)), A(y,y));
B = max(cat(3, A(x,x), A(x,y), A(y,x), A(y,y)), [], 3);
0
On

Using the Image Processing Toolbox

A = [5 4 6; 3 2 3; 8 8 9];
block_size = [2 2];
NewA = reshape(max(im2col(A, block_size, 'sliding'), [], 1), size(A)-block_size+1);

How it works:

  1. im2col(A, block_size, 'sliding') arranges each sliding submatrix of size block_size as a column;
  2. max(..., [], 1) takes the maximum of each column;
  3. reshape(..., size(A)-block_size+1) reshapes the result into a matrix of the appropriate size.

Note that steps 1 and 3 both use column-major order, so the maxima in the result are arranged consistently with the input data.

Without the toolbox

Using linear indexing and implicit expansion, im2col's behaviour can be emulated as follows:

A = [5 4 6; 3 2 3; 8 8 9];
block_size = [2 2];
ind_base = (1:block_size(1)).' + (0:block_size(2)-1)*size(A,1);
ind_corner = (1:size(A,1)-block_size(1)+1).'+ (0:size(A,2)-block_size(2))*size(A,2);
ind_cols = ind_base(:) + ind_corner(:).' - 1;
NewA = reshape(max(A(ind_cols), [], 1) , size(A)-block_size+1);

The three variables ind_base, ind_corner and ind_cols have the following interpretation:

  • ind_base defines the linear indices of the first (uppermost, leftmost) submatrix;
  • ind_corner defines the linear indices of the upper-left corner of each submatrix;
  • ind_cols contains the linear indices of each submatrix arranged as columns.
0
On

Using For-Loops (sliding window method)

Uses a set of nested for-loops to grab a neighbourhood of 4 (2 by 2). The position of the neighbourhood it relative to the variables Row_Index and Column_Index which are incremented/index the for-loop.

A = [5 4 6;
     3 2 3;
     8 8 9]; 
 
[Number_Of_Rows,Number_Of_Columns] = size(A);
Result = zeros(Number_Of_Rows-1,Number_Of_Columns-1);

for Row_Index = 1: Number_Of_Rows - 1
   for Column_Index = 1: Number_Of_Columns - 1

    Window = A(Row_Index:Row_Index+1,Column_Index:Column_Index+1);
       
    Maximum = Window(1);
    for Index = 2: 4
    if Window(Index) > Maximum
    Maximum = Window(Index); 
    end
    end
            
    Result(Row_Index,Column_Index) = Maximum;
   end
end

Result

Ran using MATLAB R2019b