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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by