How do I create a vector of 20 elements with the numbers 1,2,3,4,5 repeated four times?

16 ビュー (過去 30 日間)
Krithika Karri
Krithika Karri 2012 年 11 月 13 日
It has to look like this [0 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]
Any suggestions?
Thanks
  2 件のコメント
John D'Errico
John D'Errico 2012 年 11 月 13 日
編集済み: John D'Errico 2012 年 11 月 13 日
But the vector you show has 21 elements, not 20. Until you learn to be accurate in your questions, intelligent answers will be difficult to come by.
Jan
Jan 2012 年 11 月 13 日
編集済み: Jan 2012 年 11 月 13 日
We find 3 smart answers inspite of the incorrect question. In theory, this is not possible. But in practice, the forum can overcome such limitations. It is interesting where the missing information could come from. I think it is the experience from other questions and own programming projects.
@Krithika Karri: Please use meaning ful tags. All questions in this forum conern "matlab".

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

回答 (3 件)

Akiva Gordon
Akiva Gordon 2012 年 11 月 13 日
You can create a smaller vector of the repeating entries and then repeat this vector with the REPMAT function for 1 row and 4 columns:
smallVector = [1 2 3 4 5];
result = repmat(smallVector,1,4);
If you need to put that 0 at the front, you can then enter:
result = [0 result];

Casey
Casey 2012 年 11 月 13 日
編集済み: Casey 2012 年 11 月 13 日
Type x = [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]; into the matlab prompt
x is now the above 20 by 1 matrix, which you can basically use as a vector depending on your application.
y = x'; will make y a 1 by 20 matrix

Andrei Bobrov
Andrei Bobrov 2012 年 11 月 13 日
result = [0,mod(0:19,5)+1];

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by