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 Solvers272
Suggested Problems
-
Project Euler: Problem 6, Natural numbers, squares and sums.
2337 Solvers
-
We love vectorized solutions. Problem 1 : remove the row average.
838 Solvers
-
Reverse the elements of an array
1055 Solvers
-
Height of a right-angled triangle
1885 Solvers
-
Solve a System of Linear Equations
12938 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!