How to index time interval more efficiently?

Hi Matlab Coder, The objective is to index each of the matrix, as follow

where_owl = [8  9  32  33  56  57]
owl_hide =  [9 20  33  44  57  68]
owl_awake = [0  8  24  32  48  56]

How to make following code more compact and efficient, especially the for-loop. Thanks in advance

s.days                              = 3;
s.sph                               = 3600;
s.hpd                               = 24;
s.mph                               = 60;
owl_awake = zeros(1,10);
owl_hide = zeros(1,10);
where_owl = zeros(1,10);
total_hour = 2 * s.days  *s.hpd;
last_iteration = total_hour/s.hpd;
s.sleeptime_time =zeros (1,last_iteration);
a=0;
%%%%%   Case 1: Timing| All unit in seconds
for i = 1:2:last_iteration
  a=a+1;
  % owl_awake 0000 to 0800 every day
  owl_awake (:,i) = ((a-1)*s.hpd);
  owl_awake (:,(i+1))= (8+ ((a-1)*s.hpd));
  % owl_hide between 0900 and 2000
  owl_hide (:,i)          = (9 + ((a-1)*s.hpd));
  owl_hide (:,(i+1))      = (20 + ((a-1)*s.hpd));
  % where is the owl
  where_owl (:,i)       = (8 + ((a-1)*s.hpd)) ;
  where_owl (:,(i+1))   = (9 + ((a-1)*s.hpd));
end

1 件のコメント

jpai
jpai 2017 年 6 月 28 日
Do you mind describing what this code is supposed to do?

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

 採用された回答

David Goodmanson
David Goodmanson 2017 年 6 月 28 日
編集済み: David Goodmanson 2017 年 6 月 28 日

1 投票

Hi balandong,
Here is one way to accomplish this. At present the function in the script below has no semicolons at the end of a couple of lines so it illustrates what is going on.
s.days = 3
owl_awake = owl(s.days,[0 8])
owl_hide = owl(s.days,[9 20])
% etc.
function hoursvec = owl(ndays,interval)
rep24 = 24*(0:ndays-1);
[a b] = meshgrid(rep24,interval)
apb = (a+b)
hoursvec = apb(:)';
end
If I may ask, what is the owl doing between the hours of 2000 and 2400?

2 件のコメント

balandong
balandong 2017 年 6 月 29 日
Thanks David, this code is more compact and generalized. To answer you question, the owl repeatedly say Thank You between the hours of 2000 and 2400!. Cheers
David Goodmanson
David Goodmanson 2017 年 6 月 29 日
:)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

2017 年 6 月 28 日

編集済み:

2017 年 6 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by