对2D矩阵中的块求和-MATLAB [英] Sum over blocks in a 2D matrix - MATLAB
问题描述
我正在研究Matlab,想知道如何在大型矩阵中添加术语.具体来说,我有一个4914x4914
矩阵,并想创建一个189x189
矩阵,其中每个项等于每个26x26
子集中的项之和.
I'm working on Matlab and was wondering how I add terms within a large matrix. Specifically, I have a 4914x4914
matrix and would like to create a 189x189
matrix, where each term is equal to the sum of the terms in each 26x26
subset.
为说明起见,假设我具有如下神奇的4x4
矩阵:
To illustrate, say I had the magic 4x4
matrix as follows:
[16 2 3 13;
5 11 10 8;
9 7 6 12;
4 14 15 1]
,我想创建一个2x2
矩阵,该矩阵等于原始魔术4x4
中每个2x2
矩阵的总和,即:
and I wanted to create a 2x2
matrix equal to the sum of each 2x2
matrix within the original magic 4x4
, i.e.:
[(16+2+5+11) (3+13+10+8);
(9+7+4+14) (6+12+15+1)]
感谢您的任何建议! 谢谢 杰克
Grateful for any advice! Thanks jake
推荐答案
另一种方法是将整个矩阵重塑为4D矩阵并将第一维和第三维上的元素求和:
An alternative way is to reshape the whole matrix into a 4D matrix and sum the elements over first and third dimension:
result = squeeze(sum(sum(reshape(A,26,189,26,189),1),3));
这篇关于对2D矩阵中的块求和-MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!