Increasing efficiency of loop that uses datenum function?
Question
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
Solution
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');
OTHER TIPS
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'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow