vectorize a loop or two so that the program runs faster

2 Ansichten (letzte 30 Tage)
Osita Onyejekwe
Osita Onyejekwe am 25 Jul. 2017
Kommentiert: Jan am 25 Jul. 2017
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 Kommentare
Stephen23
Stephen23 am 25 Jul. 2017
"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
Jan am 25 Jul. 2017
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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by