Matlab 如何按块计算列的平均值?

Matlab 如何按块计算列的平均值?,matlab,matrix,average,Matlab,Matrix,Average,例如,我有以下矩阵: [1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4] 我怎么能以编程的方式,比如说,每四列计算一次平均值,这样我就可以 与: ? 谢谢 汤姆一个简单的方法就是滥用: 给出: C = 2.5000 2.5000 2.5000 2.5000 2.5000 2.5000 这似乎比所有的重塑和挤压动作都要快一点;)但无需验证。无需使用以下循环即可完成: 如果您有图像处理工具箱,还可以

例如,我有以下矩阵:

[1 2 3 4 1 2 3 4

1 2 3 4 1 2 3 4

1 2 3 4 1 2 3 4]
我怎么能以编程的方式,比如说,每四列计算一次平均值,这样我就可以 与:

?

谢谢


汤姆

一个简单的方法就是滥用:

给出:

C =

    2.5000    2.5000
    2.5000    2.5000
    2.5000    2.5000

这似乎比所有的重塑和挤压动作都要快一点;)但无需验证。

无需使用以下循环即可完成:


如果您有图像处理工具箱,还可以使用:


一种基于
sum
重塑的方法-

reshape(sum(reshape(A,size(A,1),N,[]),2)/N,size(A,1),[])

这里介绍了迄今为止发布的解决方案的一些基准

基准测试代码-

m = 1000; %// No. of rows in input
n = 4000; %// No. of cols in input
A = rand(m,n); %// Input with random data
N = 4; %// number of columns to average;
num_runs = 200; %// No. of iterations

disp('----------------- With sum+reshape');
tic
for k1 = 1:num_runs
    out1 = reshape(sum(reshape(A,size(A,1),N,[]),2)/N,size(A,1),[]);
end
toc,clear out1

disp('----------------- With mean+reshape');
tic
for k1 = 1:num_runs
    out2 = squeeze(mean(reshape(A,size(A,1), N, []), 2));
end
toc,clear out2

disp('----------------- With blockproc');
tic
for k1 = 1:num_runs
    out3 = blockproc(A, [size(A,1) N], @(x) mean(x.data, 2));
end
toc,clear out3

disp('----------------- With filter');
tic
for k1 = 1:num_runs
    B = filter(ones(1,N)/N,1,A,[],2);
    out4 = B(:,N:N:end);
end
toc,clear out4 B
结果-

----------------- With sum+reshape
Elapsed time is 4.011844 seconds.
----------------- With mean+reshape
Elapsed time is 3.945733 seconds.
----------------- With blockproc
Elapsed time is 59.018457 seconds.
----------------- With filter
Elapsed time is 31.220875 seconds.

有趣的三维重塑和重新挤压方法;)@托马斯,谢谢!我正考虑删除我的答案,因为你的答案是第一个,但后来你删除了它。为什么?我注意到我在结果中交换了列/行,你的答案是正确的。@Thomas,你说得对。只是个打字错误,现在应该可以了,现在可以了。我认为整个问题现在都得到了很好的探讨;)使用
filter
conv
是一种有趣的方法,但它计算的结果是所需结果的
N
倍(然后必须使用
N:N:end
进行下采样)+1,尽管+1使用
sum
而不是慢速
mean
是个好主意。很好的基准测试@路易斯门多:是的,似乎使用基本的方法真的能给我们带来表演+我已经为你准备好了!忘了提到
挤压
也会挤压性能@Divakar是个不错的主意,但对我来说(Matlab2011b,Win7)平均+重塑比其他人更可靠。另一个快速解决方案是
显示([2.52.5;2.52.5;2.52.5])
:D@LuisMendo所以我运行了很多次迭代,现在似乎在平均+整形和总和+整形之间是并驾齐驱的!很抱歉之前的混淆:)@Divakar在所有其他条件相同的情况下(这里的情况并不完全相同),
mean
应该稍微慢一点,因为它在调用
sum
result = blockproc(A, [size(A,1) N], @(x) mean(x.data, 2))
reshape(sum(reshape(A,size(A,1),N,[]),2)/N,size(A,1),[])
m = 1000; %// No. of rows in input
n = 4000; %// No. of cols in input
A = rand(m,n); %// Input with random data
N = 4; %// number of columns to average;
num_runs = 200; %// No. of iterations

disp('----------------- With sum+reshape');
tic
for k1 = 1:num_runs
    out1 = reshape(sum(reshape(A,size(A,1),N,[]),2)/N,size(A,1),[]);
end
toc,clear out1

disp('----------------- With mean+reshape');
tic
for k1 = 1:num_runs
    out2 = squeeze(mean(reshape(A,size(A,1), N, []), 2));
end
toc,clear out2

disp('----------------- With blockproc');
tic
for k1 = 1:num_runs
    out3 = blockproc(A, [size(A,1) N], @(x) mean(x.data, 2));
end
toc,clear out3

disp('----------------- With filter');
tic
for k1 = 1:num_runs
    B = filter(ones(1,N)/N,1,A,[],2);
    out4 = B(:,N:N:end);
end
toc,clear out4 B
----------------- With sum+reshape
Elapsed time is 4.011844 seconds.
----------------- With mean+reshape
Elapsed time is 3.945733 seconds.
----------------- With blockproc
Elapsed time is 59.018457 seconds.
----------------- With filter
Elapsed time is 31.220875 seconds.