for loop for two particular days of a week
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to make a for loop counting each monday and thursay for some period of time like one year. I want to start with coming Monday, 08/08/2022 counting it as 1 then Thursday 4, next monday 8, next Thursday 11 and upto a total of 100 Monday plus Thursday.
The vector looks like this V=[1 4 8 11 ......................]. It is going to be inconvinent to write all elements of this vector if a long period of time like 2 years choosen. I am trying to use a for loop and generalize above vector.
Any suggestions will be highly apperciated.
Thanks
Dharma
0 个评论
采纳的回答
sudobash
2022-8-7
Hi!
As per my understanding, you want to create a vector of the following sequence: 1, 4, 8, 11, 15, 18,...
This is how you could do it using a while loop:
vec = [1];
while length(vec) < 100
% Check if previous element was Monday
if mod(length(vec),2) == 1
% Add 3 to last element to get Thursday
vec = [vec vec(end)+3];
else
% Add 4 to last element to get back Monday
vec = [vec vec(end)+4];
end
end
vec
Hope this solves your problem.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!