对二维矩阵中的块求和 - 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));
这篇关于对二维矩阵中的块求和 - MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!