Azzera filtri
Azzera filtri

Add some months into a Column of Single Years

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Stephan
Stephan il 29 Dic 2018
Modificato: Stephan il 29 Dic 2018
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

Più risposte (1)

JohnB
JohnB il 29 Dic 2018
Hey Thanks Stephan, it works !

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by