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
Show
6 older comments
matlaber2021
on 17 Jan 2019
You can also use class definition to solve it, amazing!
Alex
on 31 Aug 2020
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()]));
:
Tianyang Wu
on 11 Jan 2022
One way to solve this problem:
https://paste.ubuntu.com/p/nCFk9p7XNM/
Solution Comments
Show commentsProblem Recent Solvers272
Suggested Problems
-
Select every other element of a vector
34482 Solvers
-
4609 Solvers
-
273 Solvers
-
1373 Solvers
-
Change the sign of even index entries of the reversed vector
583 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!