简体   繁体   中英

How to preallocate a datetime array in matlab

I would like to know if there is a way to pre-allocate a datetime array in Matlab.

I explain my problem: I need to read several dates (let suppose they are 10) from many files (let say they are 100) and I would like to store it in a datetime array 10X100. If my data was simple numeric date (ie double ) I can pre-allocate an array as: DataTable = zeros(10,100) . I would like to do something similar with datetime data type but I was not able to find how, can you help me?

Possible workaround/solution found

I made some tests and I found this workaround to mayproblem:

DataTable = repmat(datetime(0,0,0), 10, 100);

This way I have from the beginning an array 10X100 of the type datetime initialized to a default value. I posted this edit to the question in case it can be helpful for someone

You can initialize an empty one:

DataTable = datetime([],[],[]);

and then find some smart way to fill it. This means that you can add elements dynamically depending on your algorhitm needs.

Are you sure it makes sense to have a fixed array with all zeros?

Edit (better solution):

Another option I discovered recently is using the NaT() function that was introduced in R2015b (which @csanchisb also mentioned in his answer). NaT stands for "not a time", similar to NaN for floating point values. This is probably the best solution since it uses a built-in function that can do exactly what you want, and also makes it clear which entries of the matrix have not been set to valid datetime values.

DataTable = NaT(10,100);

After running the above line of code, you can loop through and overwrite each entry in DataTable with the actual datetime objects as you read them in.

Original answer:

I think the solution posted by the author using repmat() is probably best in most cases, but here is another possible solution:

DataTable = datetime(zeros(10,100), zeros(10,100), zeros(10,100));

or

DataTable = zeros(10,100);
DataTable = datetime(DataTable, DataTable, DataTable);

I wrote some quick code to compare the performance of this with the author's repmat() method. It looks like this method is a little faster for small arrays (~100 datetime objects), but repmat() is quite a bit faster for large arrays (~1 million datetime objects).

However, since pre-allocation usually only happens once, the speed probably doesn't matter nearly as much as how semantically clear the code is. In this case, I'd still go with the repmat() solution since I think it's easier to interpret what its code is doing.

datetime(0, 0, 0) will produce 30-Nov--0001 , which is as good a default as any. The obvious way to build the array would be repmat(datetime(0, 0, 0), 10, 100) .

An alternative would be to use NaT ('not a time'), which is of type datetime and is more obviously a default value. In that case, you can initialize the array directly with NaT(10, 100) .

you can use "cell Array" like this:

DataTable = cell(10,100); 

and fill each cell using Braces.

for example: DataTable{2,3}='2015-12-2';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM