フィルターのクリア

creating a vector that repeats a number 360 times

305 ビュー (過去 30 日間)
Ruben Alfaro
Ruben Alfaro 2014 年 5 月 9 日
編集済み: Pradeep Punniyakotti 2018 年 3 月 27 日
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; ....]

回答 (5 件)

dpb
dpb 2014 年 5 月 9 日
A zillion different ways to do this--just a few of the more obvious are
v=500+zeros(360,1);
v=500*ones(360,1);
v=repmat(500,360,1);
use your imagination...

Jan
Jan 2014 年 5 月 10 日
編集済み: Jan 2014 年 5 月 10 日
And:
c = 500;
v = c(ones(360, 1));
Or:
v(1:360, 1) = 500;
[EDITED]
v = zeros(360, 1);
v(:) = 500;
  3 件のコメント
Jan
Jan 2014 年 5 月 10 日
@Image Analyst: The 1st method is an inlined repmat(500, 360, 1).
For beginners it is important to mention, that it fails when v exists before and is larger or has another type. But even experienced programmers can fail, e.g. when a piece of code is inserted by copy&paste. Therefore I avoid it.
Roberto
Roberto 2014 年 5 月 22 日
Great contribution!!!

サインインしてコメントする。


Roberto
Roberto 2014 年 5 月 9 日
編集済み: Roberto 2014 年 5 月 22 日
r = ones(1,360)*500 % row vector
c = ones(360,1)*500 % column vector
  2 件のコメント
Image Analyst
Image Analyst 2014 年 5 月 10 日
Note: ones(1,360) is a row vector, not a column vector like the poster asked for.
Roberto
Roberto 2014 年 5 月 22 日
my mistake.. :(

サインインしてコメントする。


Image Analyst
Image Analyst 2014 年 5 月 9 日
Try this
columnVector = 500 * ones(360, 1);
  2 件のコメント
Sean de Wolski
Sean de Wolski 2014 年 5 月 22 日
I'd expect addition o be faster than multiplication.
zeros(360,1)+500
dpb
dpb 2014 年 5 月 22 日
編集済み: dpb 2014 年 5 月 22 日
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.

サインインしてコメントする。


Pradeep Punniyakotti
Pradeep Punniyakotti 2018 年 3 月 27 日
編集済み: Pradeep Punniyakotti 2018 年 3 月 27 日
You can try this.
linspace(500,500,360)
The number 500 will be repeated 360 times. The answer will be a column vector.
Take transpose to get row vector.

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by