column to multiple row conversion
Mostra commenti meno recenti
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 Commenti
David Hill
il 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
il 3 Mar 2020
Modificato: per isakson
il 3 Mar 2020
David Hill
il 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
il 4 Mar 2020
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Dates and Time in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!