Split Column into 3 Columns
显示 更早的评论
I have a matrix of size 1181X4. The first column in the original matrix is dates and I need to separate the first column into 3 columns that contain year, month, and day, respectively. The first column contains
20130101 20130102 20130103 20130104 20130105 ...(etc)
I need to split the first column into 3 columns so that column 1 contains:
2013 2013 2013 2013 2013 ...(etc)
(Note: the first column is not all 2013, it changes farther down.)
Column 2 should contain:
01 01 01 01 01 ...(etc)
(Note: like column 1, the values do change farther down)
Column 3 should look like:
01 02 03 04 05 ...(etc)
回答(1 个)
Alexandra Harkai
2017-3-9
If your 1181*4 matrix is m:
res = [floor(m(:,1)/10000), floor(mod(m(:,1), 10000)/100), (m(:,1))];
Alternatively, you can treat them as dates, and go from numbers to character arrays and then back to numbers:
res = datevec(num2str(m(:,1)), 'yyyymmdd');
res = res(1:3,:); % take only the first 3 columns
m(:,1) is the first column of the input matrix.
Note that these will give 3, not '03', as the result will be a numeric array.
类别
在 帮助中心 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!