필터 지우기
필터 지우기

Add some months into a Column of Single Years

조회 수: 1 (최근 30일)
JohnB
JohnB 2018년 12월 29일
답변: JohnB 2018년 12월 29일
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