Split Column into 3 Columns

조회 수: 3 (최근 30일)
Colin Starker
Colin Starker 2017년 3월 9일
답변: Alexandra Harkai 2017년 3월 9일
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
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.

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by