我在 matlab 中有一个 1028 x 18 矩阵。我想在 Matlab 中计算第一行和第二行按列值、第三行和第四行等的平均值,并得到一个具有平均值的新矩阵。
我想你想计算每对行的列平均值。将数组重塑为 2 x 18*1028/2,计算平均值(逐列运算),并将结果重塑为 1028/2 x 18:
>> x = rand(1028, 18);
>> result = reshape(x, 2, 1028/2*18);
>> result = mean(result);
>> result = reshape(result, 1028/2, 18);
与行对上的 for 循环相比,演示矢量化解决方案速度的快速测试:
>> x = rand(1028, 18);
>> tic; result1 = zeros(1028/2, 18); for ii = 1:1028/2; result1(ii,:) = mean(x((2*ii-1):(2*ii),:)); end; toc;
Elapsed time is 0.022432 seconds.
>> tic; result2 = reshape(x, 2, 1028/2*18); result2 = mean(result2); result2 = reshape(result2, 1028/2, 18); toc;
Elapsed time is 0.000388 seconds.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)