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 commentsGroup

Cody5:Easy
- 31 Problems
- 159 Finishers
- Energy of a photon
- Breaking Out of the Matrix
- The Top 5 Primes
- I Plead the Fifth
- Pentagonal Numbers
- Is this is a Tic Tac Toe X Win?
- Inscribed Pentagon?
- Circle/Pentagon Overlap
- Octoberfest festival
- Polarisation
- Missing five
- ASCII Birthday Cake
- Find the nearest prime number
- Extra safe primes
- 5 Prime Numbers
- Is this is a Tic Tac Toe X Win?
- Tick. Tock. Tick. Tock. Tick. Tock. Tick. Tock. Tick. Tock.
- MATLAB Counter
- Basic electricity in a dry situation
- How to subtract?
- Pernicious Anniversary Problem
- 5 Prime Numbers
- Is it really a 5?
- The glass half full
- Pi Digit Probability
- A Simple Tide Gauge with MATLAB
- Predicting life and death of a memory-less light bulb
- Write c^3 as sum of two squares a^2+b^2
- Van Eck's Sequence's nth member
- Sums of Multiple Pairs of Triangular Numbers
- Recaman Sequence - I
- Recaman Sequence - II
- Spot the First Occurrence of 5
- Energy of a photon
Problem Recent Solvers270
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!