Concatenating 2 vectors into 1 vector with the values adjacent to each other

2 views (last 30 days)
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!
  3 Comments
Moey Rojas
Moey Rojas on 30 May 2020
Most of my code has been a derivative of yearmonthCO2=[yr, mon, pCO2]. What I'm trying to do is get a [yearmonth, pCO2] output.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 30 May 2020
Edited: Star Strider on 30 May 2020
Yes. Use datetime to create them.
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 Comments
Tommy
Tommy on 31 May 2020
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
Star Strider on 31 May 2020
I decided to keep it posted!
The one glitch is that ‘PPCO2’ should have been ‘pCO2’. (And I was a Chemistry major!)
.

Sign in to comment.

More Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 30 May 2020
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
Tommy on 30 May 2020
Edited: Tommy on 30 May 2020
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
...

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!