Effacer les filtres
Effacer les filtres

Add some months into a Column of Single Years

2 vues (au cours des 30 derniers jours)
JohnB
JohnB le 29 Déc 2018
Réponse apportée : JohnB le 29 Déc 2018
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

Réponse acceptée

Stephan
Stephan le 29 Déc 2018
Modifié(e) : Stephan le 29 Déc 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

Plus de réponses (1)

JohnB
JohnB le 29 Déc 2018
Hey Thanks Stephan, it works !

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by