vectorize a loop or two so that the program runs faster
1 次查看(过去 30 天)
显示 更早的评论
can someone help me vectorize a loop or two so that my program runs faster? It runs but the results take AGES.. Here is the whole code. Thank you!!!
clear all
clc
noise_Levels = 3;
number_signals = 4;
MSE_CV = zeros(number_signals,noise_Levels);
MSE_plugIN = zeros(number_signals,noise_Levels);
MSE_Mean_Max_SNR = zeros(number_signals,noise_Levels);
MSE_maxSNR = zeros(number_signals,noise_Levels);
MSE_Poly_SNR = zeros(number_signals,noise_Levels);
%for j = 1:noise_Levels
% j
kernelShape = 'Tricube';
%********************%COMPLEX SINE******************************
% w=[0.0:0.01:1-.01]';
% w=repmat(w,5,1); %repmat(w,10,1);
%
% t=[ones(100,1)];
% for i=2:5
% t=[t;i*ones(100,1)];
% end
% %stpFunc = abs(sin(2*pi*w.*t)); %+6*sigma;
% stpFunc = sin(2*pi*w.*t); %+6*sigma;
% n = length(stpFunc);
% %plot(stpFunc)
% origFun = stpFunc';
%****************************************************************
%********************SINE****************************************
% X = linspace(0,4*pi,100);
% N=length(X);
% ySignal=(X-5).^2+10;
% ySignal=sin(X);
% origFun = ySignal;
% y = origFun;
% n = length(y);
%****************************************************************
%************** Polynomial function******************************
cc = 1000;
x = [1:cc]/cc;
origFun =(x-0.5).*(x-0.25).*(x-0.75).*20+10;
y = origFun;
n = length(origFun);
%****************************************************************
bwStep = [0.001:0.001:0.004,0.005:0.01:0.2];
p = 1;
% x = [1:length(stpFunc)]/length(stpFunc);
x = [1:n]/n;
X = x;
%x = rand(n,1);
spc = 1/n;
bwSpc = round( bwStep / spc );
for j = 1:noise_Levels
j
for iLoop = 1:number_signals
[ycnoise,bwpl,yHatPlugIn,yHatCV,yHatSNRAll,yHatSNR,yHatSNRfinalBW,yHatSNRmeanBW,sigma,cvScore,cvBW] = FCN_AR_1_2_W(X',origFun',p,bwStep,x',kernelShape);
format short g
MSE_CV(iLoop,j) = (1/length(ycnoise))*sum((yHatCV(j,:) - origFun).^2);
MSE_plugIN(iLoop,j) = (1/length(ycnoise))*sum((yHatPlugIn(j,:) - origFun).^2);
MSE_Mean_Max_SNR(iLoop,j) = (1/length(ycnoise))*sum((yHatSNRmeanBW(j,:) - origFun).^2);
MSE_maxSNR(iLoop,j) = (1/length(ycnoise))*sum((yHatSNRAll(j,:) - origFun).^2);
MSE_Poly_SNR(iLoop,j) = (1/length(ycnoise))*sum((yHatSNRfinalBW(j,:) - origFun).^2);
end
end
The main part that needs to be vectorized is this part
for j = 1:noise_Levels
j
for iLoop = 1:number_signals
[ycnoise,bwpl,yHatPlugIn,yHatCV,yHatSNRAll,yHatSNR,yHatSNRfinalBW,yHatSNRmeanBW,sigma,cvScore,cvBW] = FCN_AR_1_2_W(X',origFun',p,bwStep,x',kernelShape);
format short g
MSE_CV(iLoop,j) = (1/length(ycnoise))*sum((yHatCV(j,:) - origFun).^2);
MSE_plugIN(iLoop,j) = (1/length(ycnoise))*sum((yHatPlugIn(j,:) - origFun).^2);
MSE_Mean_Max_SNR(iLoop,j) = (1/length(ycnoise))*sum((yHatSNRmeanBW(j,:) - origFun).^2);
MSE_maxSNR(iLoop,j) = (1/length(ycnoise))*sum((yHatSNRAll(j,:) - origFun).^2);
MSE_Poly_SNR(iLoop,j) = (1/length(ycnoise))*sum((yHatSNRfinalBW(j,:) - origFun).^2);
end
end
2 个评论
Stephen23
2017-7-25
"The main part that needs to be vectorized is this part"
How do you know that vectorization will help? What mlint messages do you get? Have you used the profiler? Have you tried any other ways to improve code running speed?:
Jan
2017-7-25
Omit the "format short g", because it is not useful here. This wil not acclerate the code remarkably, but keeping the loops clean is the point to start from.
It seems like
[ycnoise,bwpl,yHatPlugIn,yHatCV,yHatSNRAll,yHatSNR, ...
yHatSNRfinalBW,yHatSNRmeanBW,sigma,cvScore,cvBW] = ...
FCN_AR_1_2_W(X',origFun',p,bwStep,x',kernelShape);
does not depend on iLoop, then move it out of the for iLoop loop.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!