timer and counter function query using loops
조회 수: 14 (최근 30일)
이전 댓글 표시
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
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!