平均 Matlab 矩阵

发布于 2024-11-28 00:30:43 字数 840 浏览 1 评论 0原文

在我使用的 Matlab 程序中,我经常需要在矩阵内求平均值(插值)。最直接的方法是将矩阵与移位矩阵(avg)相加。但是,您可以使用矩阵乘法 (avg2) 执行相同的操作。我注意到在大矩阵的情况下使用矩阵乘法的情况下速度有相当大的提高。

谁能解释为什么Matlab能够比添加相同的矩阵更快地处理这个乘法?另外,使用 avg2() 相对于 avg() 可能有哪些缺点?

在这种情况下,运行时间的差异约为 6 倍 (n=500)。

function [] = speed()
%Speed test for averaging a matrix
n = 500;
A = rand(n,n);
tic
for i=1:100
    avg(A);
end
toc

tic
for i=1:100
    avg2(A);
end
toc

end

function B = avg(A,k)
if nargin<2, k = 1; end
if size(A,1)==1, A = A'; end
if k<2, B = (A(2:end,:)+A(1:end-1,:))/2; else B = avg(A,k-1); end
if size(A,2)==1, B = B'; end
end

function B = avg2(A,k)
if nargin<2, k = 1; end
if size(A,1)==1, A = A'; end
if k<2,
   m = size(A,1);
   e = ones(m,1);
   S = spdiags(e*[1 1],-1:0,m,m-1)'/2;
   B = S*A; else B = avg2(A,k-1); end
if size(A,2)==1, B = B'; end
end

In the Matlab programs I use I often have to average within a matrix (interpolation). The most straightforward way is to add the matrix and a shifted one (avg). However you could do the same operation using matrix multiplication (avg2). I noticed a considerable speed increase in the case of using matrix multiplication in the case of large matrices.

Could anyone explain why Matlab is able to process this multiplication faster than adding the same matrix? Also what are the possible downsides of using avg2() in respect to avg()?

Difference in runtime was a factor ~6 for this case (n=500).

function [] = speed()
%Speed test for averaging a matrix
n = 500;
A = rand(n,n);
tic
for i=1:100
    avg(A);
end
toc

tic
for i=1:100
    avg2(A);
end
toc

end

function B = avg(A,k)
if nargin<2, k = 1; end
if size(A,1)==1, A = A'; end
if k<2, B = (A(2:end,:)+A(1:end-1,:))/2; else B = avg(A,k-1); end
if size(A,2)==1, B = B'; end
end

function B = avg2(A,k)
if nargin<2, k = 1; end
if size(A,1)==1, A = A'; end
if k<2,
   m = size(A,1);
   e = ones(m,1);
   S = spdiags(e*[1 1],-1:0,m,m-1)'/2;
   B = S*A; else B = avg2(A,k-1); end
if size(A,2)==1, B = B'; end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2024-12-05 00:30:43

恐怕我无法回答您正在使用的函数的内部工作原理。然而,由于它们看起来过于复杂,我觉得我应该让您了解一种更简单(而且更快)的求平均值方法。

您可以改为使用内核为 [0.5;0.5] 的 conv2。我在下面扩展了您的代码:

function [A, T1, T2 T3] = speed()
%Speed test for averaging a matrix
n = 900;
A = rand(n,n);
tic
for i=1:100
    T1 = avg(A);
end
toc

tic
for i=1:100
 T2 = avg2(A);
end
toc

tic
for i=1:100
   T3 = conv2(A,[1;1]/2,'valid'); 
end
toc

if sum(sum(abs(T3-T2))) > 0
    warning('Method 3 not equal the other methods')
end
end

function B = avg(A,k)
if nargin<2, k = 1; end
if size(A,1)==1, A = A'; end
if k<2, B = (A(2:end,:)+A(1:end-1,:))/2; else B = avg(A,k-1); end
if size(A,2)==1, B = B'; end
end

function B = avg2(A,k)
if nargin<2, k = 1; end
if size(A,1)==1, A = A'; end
if k<2,
   m = size(A,1);
   e = ones(m,1);
   S = spdiags(e*[1 1],-1:0,m,m-1)'/2;
   B = S*A; else B = avg2(A,k-1); end
if size(A,2)==1, B = B'; end
end

结果:

经过的时间是 10.201399 秒。

已用时间为 1.088003 秒。

已用时间为 1.040471 秒。

如果您已经知道这一点,我深表歉意。

Im afraid I cant give you an answer to the inner workings of the functions you are using. However, as they seem overly complicated, I felt I should make you aware of an easier (and a bit faster) way of doing this averaging.

You can instead use conv2 with a kernel of [0.5;0.5]. I have extended your code below:

function [A, T1, T2 T3] = speed()
%Speed test for averaging a matrix
n = 900;
A = rand(n,n);
tic
for i=1:100
    T1 = avg(A);
end
toc

tic
for i=1:100
 T2 = avg2(A);
end
toc

tic
for i=1:100
   T3 = conv2(A,[1;1]/2,'valid'); 
end
toc

if sum(sum(abs(T3-T2))) > 0
    warning('Method 3 not equal the other methods')
end
end

function B = avg(A,k)
if nargin<2, k = 1; end
if size(A,1)==1, A = A'; end
if k<2, B = (A(2:end,:)+A(1:end-1,:))/2; else B = avg(A,k-1); end
if size(A,2)==1, B = B'; end
end

function B = avg2(A,k)
if nargin<2, k = 1; end
if size(A,1)==1, A = A'; end
if k<2,
   m = size(A,1);
   e = ones(m,1);
   S = spdiags(e*[1 1],-1:0,m,m-1)'/2;
   B = S*A; else B = avg2(A,k-1); end
if size(A,2)==1, B = B'; end
end

Results:

Elapsed time is 10.201399 seconds.

Elapsed time is 1.088003 seconds.

Elapsed time is 1.040471 seconds.

Apologies if you already knew this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文