# How to make this matrix

3 vues (au cours des 30 derniers jours)
s.v. le 2 Fév 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
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
David Goodmanson le 2 Fév 2018

Connectez-vous pour commenter.

### Réponse acceptée

Star Strider le 2 Fév 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.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Logical dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by