I am trying to optimize a Matlab code for a statistic calculation for a large array of data (1e6 values). I tried several methods, with loops or fun functions, with diff or basic math. For me this code runs in approx 8 seconds. Though, using an additional for loop runs fine 'run section' mode, but it is unstable while running the script.
Is there any way to improve the time for this code. I tried different methods for parfor, but i could not set it up in my loop. Is anyone here experienced enough with parfor to tell me how do I get out of broadcast variable issues?
%% Matlab Question
L=400000;
t_clk = rand(1, L);
t_clk = t_clk-0.5;
plot (t_clk)
%
disp(' ')
tic
N = 1000; %2000
M = length(t_clk)-N;
temp_Pkp = zeros(1, N);
temp_Pkn = zeros(1, N);
temp_Std = zeros(1, N);
myMat = zeros(1,M);
% Time to execute: 'run section' / 'run' / 'run and time'
%parfor xx = 1 :1 : N %2.3 -> broadcast variable issues
for xx = 1 :1 : N %2.3
myMat = bsxfun(@minus,t_clk(xx+1 : xx+M) , t_clk(1:M)); %% Time to execute: 8.1s / 8.2s / 7.76s
%myMat = t_clk(xx+1 : xx+M) - t_clk(1:M); %% Time to execute: 8.1s / 8s / 86s
%myMat = zeros(1,M); % not used
%for yy = 1:1:M %% Time to execute: 4.6s / 4.6s / 23.6s ('run and time' execution time is very high)
% myMat(yy) =t_clk(xx+yy) - t_clk(yy);
%end
temp_Mean= mean(myMat) ;
temp_Pkp(xx) = max(myMat(:)) - temp_Mean ; % max - min
temp_Pkn(xx) = temp_Mean - min(myMat(:)) ; % max - min
temp_Std(xx) = sqrt(sum(sum((myMat-temp_Mean).^2))/M);
end
toc
plot(temp_Std)