|
回复 1 # 321forever 的帖子
- %这是一个移位求平均值的程序
- clear;clc;
- length=1000; %信号的总长度
- rlength=100; %移动位置为100
- average= zeros(1000,1); %初始化
- aver = zeros(1000,1);
- ave = zeros(1000,1);
- signal = rand(1,1000);
- warning off
- %% 用循环
- tic;
- for i=1:901 %这个for循环有些占用时间看看看看高手有没有什么替代或是提速的方法
- average(i)=mean(signal(i:i+99));
- end
- average(902:1000)=average(901);
- toc;
- %% 用arrayfun
- tic
- aver(1:901) = arrayfun(@(x)mean(signal(x:x+99)),1:901);
- aver(902:end) = aver(901);
- toc
- % isequal(aver,average)
- %% 用hankel矩阵构造
- tic
- b = hankel(signal(1:100),[0,signal(101:end)]);
- ave(1:901) = mean(b);
- ave(902:end) = ave(901);
- toc
- isequal(aver,ave,average)
复制代码
运行结果:
- Elapsed time is 0.015726 seconds.
- Elapsed time is 0.022444 seconds.
- Elapsed time is 0.004636 seconds.
- ans =
- 1
复制代码
|
评分
-
1
查看全部评分
-
|