Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 字符串单元格数组按分隔符拆分为整数数组_Matlab_Date_Split_Cell Array - Fatal编程技术网

Matlab 字符串单元格数组按分隔符拆分为整数数组

Matlab 字符串单元格数组按分隔符拆分为整数数组,matlab,date,split,cell-array,Matlab,Date,Split,Cell Array,我有一个大小约为900k x 1的单元格数组,其中包含格式为'5/13/2015 23:53'的日期。我试图创建一个长度相同的整数数组,它只包含每个日期单元格中的小时。最快/最好的方法是什么 编辑我只能访问以下工具箱: MATLAB Version 8.6 (R2015b) Simulink Ver

我有一个大小约为
900k x 1
的单元格数组,其中包含格式为
'5/13/2015 23:53'
的日期。我试图创建一个长度相同的整数数组,它只包含每个日期单元格中的小时。最快/最好的方法是什么

编辑我只能访问以下工具箱:

MATLAB                                                Version 8.6         (R2015b)
Simulink                                              Version 8.6         (R2015b)
Control System Toolbox                                Version 9.10        (R2015b)
DSP System Toolbox                                    Version 9.1         (R2015b)
Image Processing Toolbox                              Version 9.3         (R2015b)
Instrument Control Toolbox                            Version 3.8         (R2015b)
Optimization Toolbox                                  Version 7.3         (R2015b)
Signal Processing Toolbox                             Version 7.1         (R2015b)
Simulink Control Design                               Version 4.2.1       (R2015b)
Statistics and Machine Learning Toolbox               Version 10.1        (R2015b)
Symbolic Math Toolbox                                 Version 6.3         (R2015b)
Edit2:

tic
datesmat = datevec(Dates);
hours = datesmat(:,4);
toc

tic
Hours = cell2mat(cellfun(@(x) str2double(x(end-4:end-3)), Dates, 'UniformOutput', false));
toc

Elapsed time is 90.233473 seconds.
Elapsed time is 14.168023 seconds.
你可以使用这个方法

例如:

Hour = hour({'5/13/2015 21:53', '5/13/2015 23:53'})
结果:

Hours =

    21    23

没有财务工具箱的示例:

Dates = {'5/13/2015 21:53', '5/13/2015 23:53'};

Hours = cell2mat(cellfun(@(x) str2double(x(end-4:end-3)), Dates, 'UniformOutput', false));
如果您没有财务工具箱,可以为每个日期使用一个,其第四个元素是小时


(我怀疑财务工具箱的
hour
方法会产生类似的效果。)

使用逻辑索引的矢量化解决方案:

B = char(Dates).';
f = B ==':';
x=circshift(f,-1)|circshift(f,-2);
result = str2double(reshape(B(x),2,[]).');

我目瞪口呆地站着,被纠正了。这是一个奇怪的世界,
cellfun
实际上是最快的。请注意,为了准确计时,最好使用专门为此设计的;这与如此巨大的差异并不重要。@Rotem不一定
datevec
返回一个n×6矩阵,其中只需一列。因此,整个操作只会增加无意义的开销,而您的
cellfun
解决方案只提取必要的元素。因此,我认为它使用的内存也少了很多,这可能会在矩阵这么大的情况下派上用场。以及
datevec
在引擎盖下进行大量检查和格式化输出;20行做你想做的,其余400行用于各种例外情况。检查也会使它变慢。
B = char(Dates).';
f = B ==':';
x=circshift(f,-1)|circshift(f,-2);
result = str2double(reshape(B(x),2,[]).');