creating a vector that repeats a number 360 times

1.271 views (last 30 days)
i want a way to create a vector that repeats the same number 360 times lets say my number is 500 and i want the vector to look like this...
[500; 500; 500; 500; ....]

Answers (5)

dpb on 9 May 2014
A zillion different ways to do this--just a few of the more obvious are
use your imagination...

Jan on 10 May 2014
Edited: Jan on 10 May 2014
c = 500;
v = c(ones(360, 1));
v(1:360, 1) = 500;
v = zeros(360, 1);
v(:) = 500;

Sign in to comment.

Roberto on 9 May 2014
Edited: Roberto on 22 May 2014
r = ones(1,360)*500 % row vector
c = ones(360,1)*500 % column vector

Sign in to comment.

Image Analyst
Image Analyst on 9 May 2014
Try this
columnVector = 500 * ones(360, 1);
dpb on 22 May 2014
Edited: dpb on 22 May 2014
On modern Intel FPP, I believe they're the same for FMUL and FADD. FDIV is still an order of magnitude kind of difference.
OTOH, w/ the JIT optimizer, wouldn't surprise me terribly if for a constant it didn't do either but simply translated to a memset() or similar copy, anyway. I suppose for floating point values that's less likely, but who knows how clever they've gotten??? :)
Would definitely have to do timings to confirm if were any difference and then TMW would say to not base code on the results of that for any given release as the guts can always (and do) change.

Sign in to comment.

Pradeep Punniyakotti
Pradeep Punniyakotti on 27 Mar 2018
Edited: Pradeep Punniyakotti on 27 Mar 2018
You can try this.
The number 500 will be repeated 360 times. The answer will be a column vector.
Take transpose to get row vector.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by