How to make this matrix

2 次查看(过去 30 天)
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

采纳的回答

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 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by