Problem 1035. Generate a vector like 1,2,2,3,3,3,4,4,4,4
Solution Stats
Problem Comments
-
28 Comments
function ans = GenerateVector(n)
ans = 1;
for n = 2:n
ans = [ans n*ones(1, n)];
end
could sb explain me why this code is flagged a as a bad one ?? it works !! and another thign in problem definition it is not clearly stated that the solutions must be straight code without writing a function. I wrote a function and it works well to the definition of the problem. then I am getting incorrect information after submission. dear cody team: please write more specific requirements of the problem. you will miss many misunderstandings then.
Your function has to use exactly the same name that is provided in the function template. In this case, the function must be called "you_fcn_name". You wrote a function called "GenerateVector". Change the name of your function and it should pass.
The test suite has been expanded.
Good problem.
Is there any way to avoid the for loop? I really want to but I don't see how right now.
Good one! took some time but finally did it.
Can it be done without a for loop?
Good problem.
HINT:
use command "repelem"
Solution:
A=1:n
B=repelem(A,1:n)
great problem. Interesting for beginners like me.
can we do it without loops?
good
good problem
good
one liner
Incredibly fun problem!
Easy and funny
Great and funny
looks tricky but easy to solve.
Tricky!
oh yeah, it's all coming together
What does test 1 verify? I can't pass
@利
The first test ensures that your solution does not contain the strings "assignin" or "if"; the latter, presumably, to rule out look-up solutions. As you likely found, this sort of test has an unfortunate tendency to also mess with legitimate solutions.
nice one!
Nice problem, I really have thouroughly enjoyed to the fullest!!
Took me a while
Nice problem
Interesting
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?
@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).
Very interesting
Solution Comments
Show commentsProblem Recent Solvers11446
Suggested Problems
-
Program an exclusive OR operation with logical operators
717 Solvers
-
519 Solvers
-
503 Solvers
-
Sum the numbers on the main diagonal
582 Solvers
-
5534 Solvers
More from this Author17
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!