for loops and arrays

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

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;
no I meant to say monday-sunday so every week. Right now, my array looks like this:
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
If the user inputted 2 for the 90 minute day, then i'd want the second value of every other row to be 90. and if the user inputted 3 for the 0 minute day, i'd want the third value of every row to be 0.

Sign in to comment.

Answers (1)

David Hill
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

There is no way to do this using for loops?
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 14 Feb 2022

Commented:

on 14 Feb 2022

Community Treasure Hunt

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

Start Hunting!