Add some months into a Column of Single Years

2 次查看(过去 30 天)
Hi,
I would like to transform a column of Years by adding to them some months so it would repeat the element of each row 12 times
and add the monthly subscript close to the year.
For example turn
1970
1971
to
197001
197002
197003
197004
197005
197006
197007
197008
197109
197010
197011
197012
197101
197102
...
etc

采纳的回答

Stephan
Stephan 2018-12-29
编辑:Stephan 2018-12-29
Hi,
you can use this function:
function result = years_with_months(start_year, end_year)
% Build years
years = start_year:end_year;
years_new = string(repmat(years,12,1));
% Build Months
months = split(sprintf('0%d 0%d 0%d 0%d 0%d 0%d 0%d 0%d 0%d %d %d %d',1:12)," ");
% Concatenate years and months
for m = 1:numel(years)
for n = 1:12
years_new(n,m) = strcat(years_new(n,m),months(n));
end
end
% Finish and show result
result = double(reshape(years_new,[],1));
end
Just call the function this way with the needed years:
result = years_with_months(1970,1972)
If you save the function with the name years_with_months.m in your Matlab projects folder, you can use it always by calling it the way shown above.
Best regards
Stephan

更多回答(1 个)

JohnB
JohnB 2018-12-29
Hey Thanks Stephan, it works !

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by