Problem 2012. Tony's trick for duplicating an mx1 vector n times
Without using repmat, or for loop, or concatenation, create a function to duplicate a vector, v, a specified number of times, n. v can be numeric or character. n may be 0.
Examples
If
v = 5; n = 3;
then return v1:
v1 = [5 5 5]
If a 2d matrix is supplied then the vector v will be the first column.
V = magic(4); n = 4;
then v and v1 are returned as shown below.
v = [16 5 9 4]' v1 = [16 16 16 16; 5 5 5 5; 9 9 9 9; 4 4 4 4]
Hint: use indexing
Solution Stats
Problem Comments
-
2 Comments
When did the functionality of linspace change to give an empty 1-by-0 matrix for n=0? My version is very old (R14, SP3 from 2005), and the command "linspace(1,1,0)" gives 1. Just curious.
hi - not sure, but it is similar in behaviour to ones(1,0), nan(1,0) etc
Solution Comments
Show commentsProblem Recent Solvers52
Suggested Problems
-
1995 Solvers
-
Given an unsigned integer x, find the largest y by rearranging the bits in x
1791 Solvers
-
What is the distance from point P(x,y) to the line Ax + By + C = 0?
366 Solvers
-
Return unique values without sorting
905 Solvers
-
Mysterious digits operation (easy)
277 Solvers
More from this Author11
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!