timer and counter function query using loops

2 ビュー (過去 30 日間)
Deep Patel
Deep Patel 2020 年 7 月 17 日
コメント済み: per isakson 2020 年 8 月 1 日
clc;
clear;
close all;
state = 0;
sequance = 0;
previous_state = 0;
for n = 0 : 15
time = n ;
p = 1;
input = int32(randi([0, 1], [1, p]));
comparison = isequal(input,previous_state);
previous_state = input;
if comparison == 0
state = state + 1;
sequance = 0;
else
%for zz = 0:10
sequance = sequance + 1;
%if sequance >= 2
time = time + (2^sequance/1000);
% end
%disp(state);
end
fprintf('%f %f %f %f\n',time,input,state,sequance);
end
0.000000 1.000000 1.000000 0.000000
1.000000 0.000000 2.000000 0.000000
2.002000 0.000000 2.000000 1.000000
3.004000 0.000000 2.000000 2.000000
4.008000 0.000000 2.000000 3.000000
5.016000 0.000000 2.000000 4.000000
6.000000 1.000000 3.000000 0.000000
7.002000 1.000000 3.000000 1.000000
8.000000 0.000000 4.000000 0.000000
9.002000 0.000000 4.000000 1.000000
10.000000 1.000000 5.000000 0.000000
11.002000 1.000000 5.000000 1.000000
12.004000 1.000000 5.000000 2.000000
13.008000 1.000000 5.000000 3.000000
14.016000 1.000000 5.000000 4.000000
15.000000 0.000000 6.000000 0.000000
Here, in my program the output in timer is incresing 0.004 msec. after 1 sec. but I want to incerase in particual at same sec. and print at same time.
Please help me, Thank you in advance
  1 件のコメント
per isakson
per isakson 2020 年 8 月 1 日
"but I want to incerase in particual at same sec. and print at same time." I don't get what you want. Show an example!
"timer is incresing 0.004 msec" I would say that accourding to your code it's increasing by
( 2^sequance - 2^(sequance-1) )/1000
where
sequance = ( 1, 2, 3, ... )
during sequences when comparison==true which is what your output shows

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

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 7 月 17 日
I could not figure out your rules based upon your desired output. Here is one approximation that you could adapt further.
state = 0;
sequance = 0;
previous_state = 0;
prob = 0.1;
time = 1;
for n = 0 : 15
p = 1;
%input = int32(randi([0, 1], [1, p]));
input = int32(xor(previous_state, rand() < prob));
comparison = isequal(input,previous_state);
previous_state = input;
if comparison == 0
state = state + 1;
sequance = 0;
newtime = max(ceil(time), time+1);
else
%for zz = 0:10
sequance = sequance + 1;
if sequance > 9
newtime = time + 1;
else
newtime = time + state/100 + (2^sequance/1000);
end
% end
%disp(state);
end
fprintf('%10g %10g %10g %10g %10g\n',time,input,state,sequance,comparison);
time = newtime;
end
fprintf('\n');
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 20 日
(reminder to myself)

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by