How to make this matrix

2 visualizzazioni (ultimi 30 giorni)
s.v.
s.v. il 2 Feb 2018
Risposto: Star Strider il 2 Feb 2018
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

Risposta accettata

Star Strider
Star Strider il 2 Feb 2018
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.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by