フィルターのクリア

how to reduce the size of a matrix?

7 ビュー (過去 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 Center および File ExchangeMultidimensional Arrays についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by