Problem 57695. Repeat a string (not a character array)
True string are a relatively recent addition to MATLAB, first having been available in R2016. Strings contrast with character arrays, and often allow you to work more naturally with textual data than character arrays do.
Strings support some infix operators in MATLAB. In particular, you can concatenate strings by adding them; writing "Hello " + "world" yields "Hello world". On the other hand, scalar multiplication -- multiplying a string and a natural number -- is not possible: "ho" * 3 does not equal "hohoho", and instead produces an error message (in R2022b Update 3).
So let's remedy that! Your mission, should you choose to accept it, is to write a function that accepts as its inputs a string array in_str (of any size) and a scalar n, and that outputs a string array out_str (of the same size as in_str) such that out_str(k) is, for any k between 1 and numel(in_str), equal to in_str(k) repeated n times. You may assume that nothing funny will get passed to your function --- in particular, n will always be a non-negative integer.
The problem's easy to solve, but please try to find the most natural, the most elegant, the most MATLABian solution. Good luck, and have fun!
P.S. Please don't cheat. I'll not attempt to detect cheating in the test suite, but please be a good sport.
P.P.S. MathWorks, please consider this a feature request. ;)
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers12
Suggested Problems
-
3834 Solvers
-
1629 Solvers
-
Project Euler: Problem 5, Smallest multiple
1339 Solvers
-
1167 Solvers
-
377 Solvers
More from this Author16
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!