how to reduce the size of a matrix?

조회 수: 18 (최근 30일)
DARSHAN KUMAR BISWAS
DARSHAN KUMAR BISWAS 2022년 6월 12일
답변: Image Analyst 2022년 6월 12일
Suppose I have 4 by 4 matrix or of any size and I want to make it a 2by2 matrix by taking the average of the sub matrixes.

채택된 답변

Image Analyst
Image Analyst 2022년 6월 12일
You can do this easily with conv2:
m = randi(9, 4, 4) % Sample data.
m = 4×4
5 1 8 2 9 2 1 1 8 5 6 7 4 8 4 3
sums = conv2(m, ones(2,2)/4, 'same');
means = sums(1:2:end, 1:2:end)
means = 2×2
4.2500 3.0000 6.2500 5.0000

추가 답변 (1개)

Torsten
Torsten 2022년 6월 12일
편집: Torsten 2022년 6월 12일
n = 4;
A = rand(n,n);
A_reduced = zeros(n/2,n/2);
for i=1:n/2
for j=1:n/2
Ahelp = A(2*(i-1)+1:2*i,2*(j-1)+1:2*j);
A_reduced(i,j) = mean(Ahelp(:));
end
end
A
A = 4×4
0.6054 0.9179 0.5640 0.4081 0.0255 0.3535 0.6808 0.4845 0.6431 0.2362 0.2967 0.8546 0.4578 0.2333 0.1625 0.2299
A_reduced
A_reduced = 2×2
0.4756 0.5344 0.3926 0.3859

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by