Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing efficiency of loop that uses datenum function?

Tags:

matlab

octave

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

like image 538
Jared Avatar asked Jul 09 '26 19:07

Jared


2 Answers

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');
like image 128
Dima Avatar answered Jul 12 '26 07:07

Dima


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'));
like image 24
Jared Avatar answered Jul 12 '26 08:07

Jared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!