Problem 1035. Generate a vector like 1,2,2,3,3,3,4,4,4,4
Generate a vector like 1,2,2,3,3,3,4,4,4,4
So if n = 3, then return
[1 2 2 3 3 3]
And if n = 5, then return
[1 2 2 3 3 3 4 4 4 4 5 5 5 5 5]
Solution Stats
Problem Comments
-
28 Comments
Show
25 older comments
Long Runjia Yu
on 30 Sep 2024
With the usage of for loops this becomes doable. There are similar problems where it (and while) are explicitly banned in test cases so that my solution there got errored. How do I solve this without?
Christian Schröder
on 30 Sep 2024
@Long Runjia Yu Perhaps look at other (correct) solutions to this problem, and learn from them.
Here (and elsewhere) one way to solve the problem at hand is to generate more than you need, and then throw away that which is superfluous (both without loops).
Robert
on 19 Nov 2024
Very interesting
Solution Comments
Show commentsGroup

Cody Challenge
- 12 Problems
- 465 Finishers
- Add two numbers
- Find the sum of all the numbers of the input vector
- Quote Doubler
- De-dupe
- Cell joiner
- Remove all the words that end with "ain"
- Nearest Numbers
- Find state names that start with the letter N
- Word Counting and Indexing
- Given two arrays, find the maximum overlap
- It dseon't mettar waht oedrr the lrettes in a wrod are.
- Counting in Finnish
Problem Recent Solvers11964
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!