if condition picking certain day of week

1 回表示 (過去 30 日間)
Dharma Khatiwada
Dharma Khatiwada 2022 年 8 月 3 日
コメント済み: Dharma Khatiwada 2022 年 8 月 4 日
Hi,
I am using a for loop using time as a variable. For day 1 (Monday) there is a value. Then I start a for loop starting from 4th day (Thursday). I want to use for loop only on Day 1(Monday) and Day 4 (Thursday) of every week for 40 weeks. This is related to a situation in radiation treatment where the radiation is given every Monday and Thursday.
I am using following script for 'for loop' which is obviously not true for my above situation. Any help will be appreciated.
Thanks
Dharma
syms x t
c2(x) = 2*dirac(x-1);%day 1
for j=4:4:40 %needs some modification
c2(x) = c2(x)+2*dirac(x-j);
end

回答 (1 件)

KSSV
KSSV 2022 年 8 月 3 日
thedates = (datetime(2022,01,1):datetime(2022,1,31))';
d = day(thedates,'name') ;
idx = ismember(d,{'Monday','Thursday'}) ;
d(idx)
ans = 9×1 cell array
{'Monday' } {'Thursday'} {'Monday' } {'Thursday'} {'Monday' } {'Thursday'} {'Monday' } {'Thursday'} {'Monday' }
find(idx)
ans = 9×1
3 6 10 13 17 20 24 27 31
  1 件のコメント
Dharma Khatiwada
Dharma Khatiwada 2022 年 8 月 4 日
Thank you KSSV,
As a beginner, I am still having difficulty to implement your idea in my code.
Could you help me to clarify further?
How do I use idx in place of j of following code? I want to see the j picking mondays, thursdays as a number and the maximum I want to go is upto 40 times. For example, if I count from tomorrow (thursday) as 1 then coming monday (second),..........upto 40 i.e. a total of 40 Monday plus Thursday.
syms x t
c2(x) = 2*dirac(x-1);%day 1
for j=4:4:40 %needs some modification
c2(x) = c2(x)+2*dirac(x-j);
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by