Split Column into 3 Columns

3 Ansichten (letzte 30 Tage)
Colin Starker
Colin Starker am 9 Mär. 2017
Beantwortet: Alexandra Harkai am 9 Mär. 2017
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)

Antworten (1)

Alexandra Harkai
Alexandra Harkai am 9 Mär. 2017
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.

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by