column to multiple row conversion
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
TESFALEM ALDADA
am 3 Mär. 2020
Bearbeitet: per isakson
am 5 Mär. 2020
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 Kommentare
David Hill
am 3 Mär. 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?
Akzeptierte Antwort
per isakson
am 4 Mär. 2020
Bearbeitet: per isakson
am 5 Mär. 2020
Try
col = rand(366,1); % sample data
mat = col2mat( col, 2020 );
and
col = rand(365,1); % sample data
mat = col2mat( col );
where
function mat = col2mat( col, year )
narginchk(1,2)
if nargin==1
if numel(col)==365
year = 2001; % not a leap year
elseif numel(col)==366 % Fixed error; replaced 365 by 366
year = 2000; % a leap year
else
error( 'Wrong length of col, %d.', numel(col) )
end
end
mat = nan( 31, 12 ); % pad with NaNs
len = eomday( year, 1:12 );
ix1 = [ 1, cumsum(len(1:11))+1 ]; % first day of month
ix2 = [ ix1(2:end)-1, sum(len) ]; % last day of month
for jj = 1 : 12
mat( 1:(ix2(jj)-ix1(jj)+1), jj ) = col(ix1(jj):ix2(jj));
end
end
3 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!