Concatenating 2 vectors into 1 vector with the values adjacent to each other
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I have a three vectors: month, year, and partial pressure of carbon dioxide for 2914 observations. I am trying to use a program to analyze the seasonality of the data, but the program requires that the data be in a 2914x2 (2914 rows, 2 columns) matrix such that column one is the year and month (i.e. February 2012 would be 20121 and June 1985 would be 19856) and column 2 is the partial pressure of carbon dioxide. I've tried countless times to get the data in this format but no matter what I do when I concatenate it forms a 2914x3 matrix. Is there a way to actually concatenate the year and month vectors to get in the format "yearmonth" like 20121 or 19856? Thanks so much!
채택된 답변
Star Strider
2020년 5월 30일
편집: Star Strider
2020년 5월 30일
Example —
CO2_Mtx = [2012 01 rand; 2012 2 rand; 1985 5 rand; 1985 6 rand]; % Create Matrix
YM = datetime(num2str(CO2_Mtx(:,[1 2]),'%d %d'), 'InputFormat','yyyy MM', 'Format','yyyyMM'); % Convert ‘Years Month’ To ‘datetimeâ
CO2_Tbl = table(YM,CO2_Mtx(:,3), 'VariableNames',{'YearMth','CO2PP'}) % Table Of Results
That should work with the data as you have described it. Without your actual matrix however, I cannot be certain.
Producing —
CO2_Tbl =
4×2 table
YearMth CO2PP
_______ ________
201201 0.046171
201202 0.097132
198505 0.82346
198506 0.69483
EDIT —
Added ‘CO2_Tbl’ output.
댓글 수: 5
Tommy
2020년 5월 31일
I think this is a great answer and I hope you don't delete it. I'm not quite at the point where I think datetime when I ought to, but I'm getting there, thanks in part to this answer - that's one of the main reasons I spend time on this site! I'd suggest the OP accept this answer.
Star Strider
2020년 5월 31일
I decided to keep it posted!
The one glitch is that ‘PPCO2’ should have been ‘pCO2’. (And I was a Chemistry major!)
.
추가 답변 (2개)
Bjorn Gustavsson
2020년 5월 30일
Why not simply do something like this:
Year_pressure = [year(:) + (month(:)-.5)/12,p_CO2(:)];
Here I've modified your conversion of year+month to "fractional years", mainly because I dont understand how you can map February 2012 to 20121 and June 1985 to 19856, but you can simply replace what I did for the dates with whatever you need for converting the year-month to the format you require.
HTH
Tommy
2020년 5월 30일
편집: Tommy
2020년 5월 30일
Should February 2012 map to 20122 rather than 20121? If so, maybe this will do the trick?
month = repmat((1:12)', 5, 1);
year = repelem((1998:2002)', 12, 1);
pCO2 = rand(60, 1);
data = [str2num(sprintf('%d%d\n',[year,month]')), pCO2];
(edit)
>> fprintf('%d %0.3f\n',data')
19981 0.132
19982 0.085
19983 0.232
19984 0.625
19985 0.372
19986 0.130
19987 0.679
19988 0.836
19989 0.662
199810 0.815
199811 0.924
199812 0.314
19991 0.499
...
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!