Problem 44345. MATLAB Counter
Write a function f = counter(x0,b) to construct a counter handle f that counts with an initial value x0 and a step size b.
E.g.,
>> f = counter(0,1) % Initialize a counter f() with initial_count = 0 and step_size = 1 >> f() ans = 0 >> f() ans = 1 >> f() ans = 2
Solution Stats
Problem Comments
-
9 Comments
You can also use class definition to solve it, amazing!
the tests can be strengthened a bit, to allow multiple counters
g = counter(1,2);
h = counter(3,5);
assert(isequal([1 3 5 3 8 13], [g() g() g() h() h() h()]));
:
One way to solve this problem:
https://paste.ubuntu.com/p/nCFk9p7XNM/
Solution Comments
Show commentsProblem Recent Solvers270
Suggested Problems
-
Make the vector [1 2 3 4 5 6 7 8 9 10]
50164 Solvers
-
Find all elements less than 0 or greater than 10 and replace them with NaN
15609 Solvers
-
middleAsColumn: Return all but first and last element as a column vector
613 Solvers
-
Predicting life and death of a memory-less light bulb
326 Solvers
-
Is this a valid Tic Tac Toe State?
120 Solvers
More from this Author29
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!