我正在研究Matlab,并想知道如何在一个大矩阵中添加术语。具体来说,我有一个4914x4914
矩阵,并希望创建一个189x189
矩阵,其中每个项等于每个26x26
子集中的术语总和。
为了说明,我说我有如下的魔术4x4
矩阵:
[16 2 3 13;
5 11 10 8;
9 7 6 12;
4 14 15 1]
我想创建一个2x2
矩阵,该矩阵等于原始魔法2x2
中每个4x4
矩阵的总和,即:
[(16+2+5+11) (3+13+10+8);
(9+7+4+14) (6+12+15+1)]
感谢任何建议! 谢谢 杰克
答案 0 :(得分:12)
假设A
是输入4914x4914
矩阵,这可能是一种有效的(在运行时方面)方法 -
sublen = 26; %// subset length
squeeze(sum(reshape(sum(reshape(A,sublen,[])),size(A,1)/sublen,sublen,[]),2))
对于通用块大小,让我们有一个函数 -
function out = sum_blocks(A,block_nrows, block_ncols)
out = squeeze(sum(reshape(sum(reshape(A,block_nrows,[])),...
size(A,1)/block_nrows,block_ncols,[]),2));
return
示例运行 -
>> A = randi(9,4,6);
>> A
A =
8 2 4 9 4 5
3 3 8 3 6 8
9 6 6 7 1 9
4 5 5 7 1 2
>> sum_blocks(A,2,3)
ans =
28 35
35 27
>> sum(sum(A(1:2,1:3)))
ans =
28
>> sum(sum(A(1:2,4:6)))
ans =
35
>> sum(sum(A(3:4,1:3)))
ans =
35
>> sum(sum(A(3:4,4:6)))
ans =
27
如果您想避免squeeze
-
sum(permute(reshape(sum(reshape(A,sublen,[])),size(A,1)/sublen,sublen,[]),[1 3 2]),3)
希望您关心性能,以下是此处发布的所有解决方案的基准测试结果。我使用的基准测试代码 -
num_runs = 100; %// Number of iterations to run benchmarks
A = rand(4914);
for k = 1:50000
tic(); elapsed = toc(); %// Warm up tic/toc
end
disp('---------------------- With squeeze + reshape + sum')
tic
for iter = 1:num_runs
sublen = 26; %// subset length
out1 = squeeze(sum(reshape(sum(reshape(A,sublen,[])),...
size(A,1)/sublen,sublen,[]),2));
end
time1 = toc;
disp(['Avg. elapsed time = ' num2str(time1/num_runs) ' sec(s)']), clear out1 sublen
disp('---------------------- With kron + matrix multiplication')
tic
for iter = 1:num_runs
n = 189; k = 26;
B = kron(speye(k), ones(1,n));
result = B*A*B';
end
time2 = toc;
disp(['Avg. elapsed time = ' num2str(time2/num_runs) ' sec(s)']),clear result n k B
disp('---------------------- With accumarray')
tic
for iter = 1:num_runs
s = 26; n = size(A,1)/s;
subs = kron(reshape(1:(n^2), n, n),ones(s));
out2 = reshape(accumarray(subs(:), A(:)), n, n);
end
time2 = toc;
disp(['Avg. elapsed time = ' num2str(time2/num_runs) ' sec(s)']),clear s n subs out2
我在我的系统上得到的基准测试结果 -
---------------------- With squeeze + reshape + sum
Avg. elapsed time = 0.050729 sec(s)
---------------------- With kron + matrix multiplication
Avg. elapsed time = 0.068293 sec(s)
---------------------- With accumarray
Avg. elapsed time = 0.64745 sec(s)
答案 1 :(得分:5)
如果您没有图像处理工具箱,则可以使用accumarray
:
s = 26;
n = size(A,1)/s;
subs = kron(reshape(1:(n^2), n, n),ones(s));
reshape(accumarray(subs(:), A(:)), n, n)
如果你决定聚合某种方式而不是简单的总和,这是可以重复的,例如中位数:
reshape(accumarray(subs(:), A(:), [], @median), n, n)
答案 2 :(得分:5)
当然可以使用矩阵乘法:
n = 26;
k = 189;
B = kron(speye(k), ones(1,n));
result = B*A*B';
答案 3 :(得分:5)
另一种方法是将整个矩阵重塑为4D矩阵,并在第一维和第三维上对元素求和:
result = squeeze(sum(sum(reshape(A,26,189,26,189),1),3));