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

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
TESFALEM ALDADA 2020년 3월 3일
편집: per isakson 2020년 3월 3일
Thank you, for the information
But, the above script doesn't convert it into 12*31 matrix of a year. I tried it using 365 days of column matrix...whats next i have to do?
Best
David Hill
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
TESFALEM ALDADA 2020년 3월 4일
Okey lets consider I have 31 days for all months (teplacing it by nan), then how would look like the new method to convert it in to row for one year,and also how can i apply it for long years.
Thank you!

댓글을 달려면 로그인하십시오.

 채택된 답변

per isakson
per isakson 2020년 3월 4일
편집: per isakson 2020년 3월 5일

0 개 추천

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

TESFALEM ALDADA
TESFALEM ALDADA 2020년 3월 4일
Ja, this pefectly works for one year data.
Then what could be changed if i am applying thus for long years?
per isakson
per isakson 2020년 3월 5일
I fixed a copy&paste error in the elseif-statement, see the answer.
TESFALEM ALDADA
TESFALEM ALDADA 2020년 3월 5일
편집: per isakson 2020년 3월 5일
ok

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2020년 3월 3일

편집:

2020년 3월 5일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by