how to reduce the size of a matrix?

11 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Image Analyst
Image Analyst il 12 Giu 2022
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

Più risposte (1)

Torsten
Torsten il 12 Giu 2022
Modificato: Torsten il 12 Giu 2022
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

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by