简体   繁体   中英

Creating dataset from matrix in Matlab

I'm trying to create a dataset from a double matrix and cell array of labels.

I don't have access to the mat2dataset function so I'm trying to write something similar.

>> whos data feature_labels
  Name                Size             Bytes  Class     Attributes
  data                2x208             3328  double              
  feature_labels      1x208            50776  cell   

In actual use the data will have ~2million rows and always be double format. The number of columns will range from 20 up to 2000, so doing something like;

>> D = dataset([],[],[],[],[],...[], 'VarNames', feature_labels); 

isn't really feasible.

Any suggestions?

edit:

Currently using a for loop and horzcat to concatenate new dataset columns on each loop. I don't see a way to pre-allocate the dataset size is this way so I imagine performance will chug with the larger datasets though..

Have you considered using a struct? I use these all the time in MATLAB for database things. I know it works absolutely fantastic for up to 20,000 elements with about 15 fields each, so I think it would still work just as well as anything else for 2 million items with 2 fields.

Alternatively, can't you just put it in a cell array?

DataBase{rowNum,1}=dataVector(rowNum,:);
DataBase{rowNum,2}=label{rowNum};

To preallocate a struct or cell, its relatively easy, with a struct, once you make your first one to initialize the fields, just say Struct(2000000).fieldName =[]

TO preallocate your cell array, just do

DataBase={[]}
DataBase{2000000,2}=[]

This preallocates all of it and fills it with empty values.

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