Help with adding a column with numbers that are repeated to a matrix

1 回表示 (過去 30 日間)
Samy Ben Thabet
Samy Ben Thabet 2018 年 11 月 29 日
回答済み: Adam Danz 2018 年 11 月 29 日
Hi,
I have a 2170x1 matrix I need to create a new 2170x2 matric where I add numbers going from 1 to 12 in the second column all the way to row 2170. The thing is that when 12 is reached I need it to start at 1 again and so on until the end of my matrix is reached but I can't figure out how to do that.

採用された回答

Adam Danz
Adam Danz 2018 年 11 月 29 日
If 'm' is your 2170x1 matrix,
pattern = [1:12]';
patternRep = repmat(pattern, ceil(size(m,1) / length(pattern)), 1);
m(:,2) = patternRep(1:size(m,1));

その他の回答 (1 件)

Matt J
Matt J 2018 年 11 月 29 日
編集済み: Matt J 2018 年 11 月 29 日
Let's say your matrix is A,
A(:,2)=mod(0:2169,12)+1;

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by