column to multiple row conversion
Show older comments
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 Comments
David Hill
on 3 Mar 2020
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
on 3 Mar 2020
Edited: per isakson
on 3 Mar 2020
David Hill
on 3 Mar 2020
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
on 4 Mar 2020
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!