column to multiple row conversion
이전 댓글 표시
Hi,
I wanted to convert a column vector in to multiple rows. The target is i have column of 1 year precipitation data (365,1) and i wonted to convert this in to 12*31 (month by days) matrix including leap years.
Who can can help me with this?
With kind regards,
Tesfalem,
댓글 수: 4
David Hill
2020년 3월 3일
You will either need to add some zeros to the months that don't have 31 days to keep it in a matrix, or use a cell array. For the case using a cell array:
y=cumsum([0 31 28 31 30 31 30 31 31 30 31 30 31]);
ly=cumsum([0 31 29 31 30 31 30 31 31 30 31 30 31]);
if length(data)==365
for k=1:12
m{k}=data(y(k)+1:y(k+1))';
end
else
for k=1:12
m{k}=data(ly(k)+1:ly(k+1))';
end
end
TESFALEM ALDADA
2020년 3월 3일
편집: per isakson
2020년 3월 3일
David Hill
2020년 3월 3일
The above is for a cell array. If you want a matrix you are going to have to pad the months that have less than 31 days with some value (zero maybe or nan). What are you trying to do? Why would a cell array not work?
TESFALEM ALDADA
2020년 3월 4일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!