I have a 32 * 32 matrix .i want to take mean of each 4*4 so that the matrix is reduced to 8*8 array. Can anyone help me how to do that?

조회 수: 10 (최근 30일)
please help me how to take mean of specified dimension from a large array.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 10월 20일
편집: Andrei Bobrov 2017년 10월 20일
Try use blockproc from Image Processing Toolbox
out = blockproc(A,[4 4],@(x)mean(x.data(:)));
variant without Image Processing Toolbox
[m,n] = size(A);
k = [4 4];
[ii, jj] = ndgrid(ceil((1:m)/k(1)),ceil((1:n)/k(2)));
out1 = accumarray([ii(:),jj(:)],A(:),[],@mean);
other variant
[m,n] = size(A);
k = [4 4];
C = mat2cell(A,k(1)*ones(m/k(1),1),k(2)*ones(n/k(2),1));
out = cellfun(@(x)mean(x(:)),C);
  댓글 수: 2
bidlee devi
bidlee devi 2020년 2월 12일
out = blockproc(A,[4 4],@(x)mean(x.data(:)));
I got the desired results. Reduction of 32by32 to 8by8.
How can I subtract each elements of 4by4 of A from its respective means? Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2017년 10월 20일
A = rand(32,32) ;
m = 4 ; n = 4 ;
l = size (A) ./ [m n];
T = mat2cell (A, repmat (m, l(1), 1), repmat (n, l (2), 1));
M = cellfun(@mean,T,'un',0) ;
M = cellfun(@mean,M,'un',0) ;
M = cell2mat(M)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by