Effacer les filtres
Effacer les filtres

Optimizing a 'for' loop

3 vues (au cours des 30 derniers jours)
djr
djr le 10 Fév 2015
Commenté : Titus Edelhofer le 10 Fév 2015
Hello,
I have a cell containing two cell columns. Column 1 is date and Column 2 is hour. I want to merge them together and transfrom date/time to the Matlab format. I am using a 'for' loop to do this, because I wasn't able to do it using 'cellfun'. This is the code:
for i=1:385521
PT{1}{i,1} = datenum(strcat(PT{1,1}{i,1},PT{1,2}{i,1}),'dd.mm.yyyyHH');
end
This works, but it takes about 6 minuts to do this operation. Could you give me some suggestions on how to optimize this operation. Even better if it is not in a loop.
Thanks, DjR
  1 commentaire
djr
djr le 10 Fév 2015
This is the sample of the input file (first few lines) and the whole for loop:
fid = fopen('test.txt');
PT = textscan(fid, '%s %s %f %f','Delimiter',',', 'HeaderLines',1);
fclose(fid);
clear ans fid
% Organize data (remove 00:00:00) and convert date to Matlab format
PT{1} = cellfun(@(x) strrep(x, ' 00:00:00',''), PT{1},'UniformOutput',false);
PerRow = cell2mat(strfind(PT{:,1},'.'));
for i=1:length(PT{:,1})
% add two zeros if format is m.d.yyyy
if PerRow(i,1) == 2 && PerRow(i,2) == 4
PT{1}{i,1} = ['0', PT{1,1}{i,1}(1:2), '0', PT{1}{i,1}(3:end)];
% Add one zero if format is d.mm.yyyy
elseif PerRow(i,1) == 2 && PerRow(i,2) ~=4
PT{1}{i,1} = ['0', PT{1}{i,1}];
% Add one zero if format is dd.m.yyyy
elseif PerRow(i,2) == 5
PT{1}{i,1} = [PT{1}{i,1}(1:3), '0', PT{1}{i,1}(4:end)];
end
% Add zero if format is H
if size(PT{1,2}{i},2) == 1
PT{1,2}{i}=['0', PT{1,2}{i}];
end
% Combain columns and transform date/time to Matlab format
PT{1}{i,1} = datenum(strcat(PT{1,1}{i,1},PT{1,2}{i,1}),'dd.mm.yyyyHH');
end
clear i PerRow
The loop without the problematic line takes only about 20 sec on my Linux computer.

Connectez-vous pour commenter.

Réponse acceptée

Titus Edelhofer
Titus Edelhofer le 10 Fév 2015
Hi,
you should do the strcat out of the loop, something like
PT12 = strcat(PT{1,1}(:,1), PT{1,2}(:,1));
and then you don't need the loop on datenum
PTnum = datenum(PT12, 'dd.mm.yyyyHH');
Titus
  3 commentaires
djr
djr le 10 Fév 2015
Thanks a lot. It is much more efficient. It takes like 30 seconds to finish. It took about 6 minutes when the line was in the loop.
Titus Edelhofer
Titus Edelhofer le 10 Fév 2015
Your welcome. Usually MATLAB is not that bad on loops anymore. But datenum is using a MEX file in the back ground, which has some overhead when called in a (large) loop.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by