Borrar filtros
Borrar filtros

Add some months into a Column of Single Years

2 visualizaciones (últimos 30 días)
JohnB
JohnB el 29 de Dic. de 2018
Respondida: JohnB el 29 de Dic. de 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

Respuesta aceptada

Stephan
Stephan el 29 de Dic. de 2018
Editada: Stephan el 29 de Dic. de 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

Más respuestas (1)

JohnB
JohnB el 29 de Dic. de 2018
Hey Thanks Stephan, it works !

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by