Jared
Jared

Reputation: 39893

Increasing efficiency of loop that uses datenum function?

The following loop takes about 700 seconds to run in octave and 22 seconds to run in matlab when the DJI matrix has 21000 rows. How can I increase the efficiency of this?

for i=1:length(DJI)
DJI2(i,1)=datenum(char(DJI(i,2)),'yyyy-mm-dd');
end

Upvotes: 3

Views: 780

Answers (2)

Jared
Jared

Reputation: 39893

I replaced the loop with the following and got at least a one order of magnitude increase in speed.

DJI2(:,1) = reshape(datenum(strvcat(DJI(:,2)(:)), length(DJI(:,2)),'yyyy-mm-dd'));

Upvotes: 2

Dima
Dima

Reputation: 39419

Did you remember to preallocate DJI2?

More importantly, you do not need the loop at all. datenum operates on arrays. Try this:

DJI2=datenum(char(DJI(:,2)),'yyyy-mm-dd');

Upvotes: 2

Related Questions