Poppy Joshi
Poppy Joshi

Reputation: 11

Is there a way to group matrix elements in matlab?

I am working on a problem which requires me to group array elements and average each group. For example consider the 4 x 4 matrix M,

M = [ 1 0 0 1;
      1 0 1 1;
      1 1 0 1;
      0 0 0 1;]

I want to group this into a 2 x 2 matrix, taking an average of the elements so we would get

M1 = [0.5 0.75;
      0.5 0.5;]

does anyone know a way to do this?

Many thanks

Upvotes: 1

Views: 60

Answers (4)

Edric
Edric

Reputation: 25140

You can do this using conv2, and a little indexing, like so:

>> A = conv2(M,ones(2), 'same');
>> A(1:2:end,1:2:end)/4

ans =

    0.5000    0.7500
    0.5000    0.5000

Upvotes: 1

drops
drops

Reputation: 1604

You can loop over the grouped matrices and then calculate the mean

M = [ 1 0 0 1;
      1 0 1 1;
      1 1 0 1;
      0 0 0 1;];

n=2; % 2x2 mat
 
% create new matrix with means
MM = zeros(n,n);
 
% row counter for mean-Matrix
r=1;

% loop over matrix groups
for row=1:n:size(M,1)
    c = 1; % column counter for mean-Matrix
    for col=1:n:size(M,2)
        MM(r,c) = mean(mean(M(row:row+n-1, col:col+n-1)));
        c = c+1;
    end
    r = r+1;
end

Output:

MM =

0.5000    0.7500
0.5000    0.5000

Upvotes: 0

Vaclav Pelc
Vaclav Pelc

Reputation: 675

You can do something like this for any rectangle, where x and y denote the size of domains that you want to average.

function M1 = get_means(M, x, y) 
    [rows, cols] = size(M);
    if mod(rows, x) == 0 && mod(cols, y) == 0
        for i = 1:y:cols
            for j = 1:x:rows
                M1((j+x-1)/x, (i+y-1)/y) = sum(M(j:j+x-1,i:i+y-1),'all')/(x*y);
            end
        end
    else
        error('The matrix doesn''t have compatible dimensions.')
    end
end

Upvotes: 0

Jonathan
Jonathan

Reputation: 817

I think the way to go is to first split your matrix in parts using mat2cell, then apply your functions to each part and merge them to a new matrix:

>> M = [ 1 0 0 1;
1 0 1 1;
1 1 0 1;
0 0 0 1;]

M =

     1     0     0     1
     1     0     1     1
     1     1     0     1
     0     0     0     1

>> T=mat2cell(M, [2 2], [2 2])

T =

  2×2 cell array

    {2×2 double}    {2×2 double}
    {2×2 double}    {2×2 double}

>> M1 = cellfun(@mean, cellfun(@mean, T, 'UniformOutput', false))

M1 =

    0.5000    0.7500
    0.5000    0.5000

>>

Upvotes: 0

Related Questions