for loops and arrays
Show older comments
If I have an array where rows is monday-friday and columns (N) is the number of weeks user inputted, how would I be able to use for loops and change one day every other week to 90, which user will indicate which day, and one day every week to 0, which user will indicate which day?.
2 Comments
David Hill
on 14 Feb 2022
Do you have a 5x(number of weeks) matrix?
d1=input('day for 90');%could be an array
d2=input('day for 0');%could be an array [1 3 5 2 4 2 3 1]
yourMatrix(d1,1:2:end)=90;%sets days indicated every other week to 90
yourMatrix(d2,1:2:end)=0;
Owen Bots
on 14 Feb 2022
Answers (1)
David Hill
on 14 Feb 2022
Edited: David Hill
on 14 Feb 2022
m=[30 45 30 45 30 45 30
45 30 45 30 45 30 45
30 45 30 45 30 45 30
45 30 45 30 45 30 45
30 45 30 45 30 45 30
45 30 45 30 45 30 45
30 45 30 45 30 45 30 ];
d1=input('day for 90');
d2=input('day for 0');
m(1:2:end,d1)=90;
m(1:end,d2)=0
m =
30 90 0 45 30 45 30
45 30 0 30 45 30 45
30 90 0 45 30 45 30
45 30 0 30 45 30 45
30 90 0 45 30 45 30
45 30 0 30 45 30 45
30 90 0 45 30 45 30
2 Comments
Owen Bots
on 14 Feb 2022
David Hill
on 14 Feb 2022
Yes, you can do with for-loops but above is much easier and the matlab way.
d1=input('day for 90');
d2=input('day for 0');
for k=1:size(m,1)
if mod(k,2)==1
m(k,d1)=90;
end
m(k,d2)=0;
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!