timer and counter function query using loops

조회 수: 14 (최근 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');

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by