Matlab – Increasing efficiency of loop that uses datenum function

MATLABoctave

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

Best Answer

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');

Related Topic