how to improvise the code ? It is taking lot of time for compilation?

1 view (last 30 days)
clc
clear all
close all
load s03_fc200_fs500.mat
mecg = output_x3';
mfecg = output_x4';
pcg = output_x1';
mecg = mecg (251:150000);
mfecg = mfecg (251:150000);
pcg = pcg (251:150000);
N=432;
n=2500;
s = 1;
l = 1;
for i=1:n
x1= pcg(i:i+N-1);
for j=1:n
x2=pcg(j:j+N-1);
k(i,j) =(s^2)* exp(-(x1*x2')*0.5/(l^2));
end
end
abecg= mfecg(251:251+n-1);
for i=1:n
k1= k(i,:);
fecg(1,i) = k1*(inv(k))*abecg';
end
  1 Comment
Brendan Hamm
Brendan Hamm on 28 Jun 2016
The first improvement would be comments so that other people can understand what you are doing to help you. So often overlooked, yet so important.
The second is to not use the inv function. Use:
k\abceg'
instead of:
inv(k)*abecg'
Pre-allocate space for k.
k = nan(1000); % Or whatever size you need it to be
For the first loop it seems there is little you can do to improve this as for each block of N elements you must consider it with all other N-length sequences. Most suggestions to improve speed would require alot of extra memory overhead. These include things like arrayfun. I would likely do this is parallel as the order of the iterations is independent.
doc parfor

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!