how to reduce the size of a matrix?

17 次查看(过去 30 天)
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