How to make this matrix

조회 수: 1 (최근 30일)
s.v.
s.v. 2018년 2월 2일
답변: Star Strider 2018년 2월 2일
I want Matlab to create a 12x31 matrix that looks like the result of the following code.
M = [1:31; 32:59,0 0 0; 60:90; 91:120,0; 121:151; 152:181,0; 182:212; 213:243; 244:273,0; 274:304; 305:334,0; 335:365]
However, I want Matlab to calculate the element values and place the zeros accordingly, rather than me calculating the ranges and placing the zeros. Is this possible? Basically, I want a more simple code to produce this same result. Thanks
  댓글 수: 1
David Goodmanson
David Goodmanson 2018년 2월 2일
what about leap year?

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

채택된 답변

Star Strider
Star Strider 2018년 2월 2일
This is as efficient as I can get get the code to calculate the matrix:
v = eomday(2000, (1:12)'); % Choose A Leap Year
Ms = zeros(12,31); % Preallocate Output Matrix
Mb = bsxfun(@plus, cumsum(ones(12,31),2), cumsum([0; v(1:11)])); % Calculate Cumulative Day Matrix
for k1 = 1:size(Ms,1)
Ms(k1, 1:v(k1)) = Mb(k1, 1:v(k1)); % Assign Columns By Month (Row)
end
The loop is necessary because I could not find any other way to do the column addressing for each row.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by