How can I make a column matrix of different sequential elements.

1 回表示 (過去 30 日間)
Zishan
Zishan 2014 年 6 月 23 日
回答済み: Roger Stafford 2014 年 6 月 23 日
I want to make vector of 20 rows and 1 column suppose this vector [1;1;1;1;2;2;2;2;2;2;3;3;4;4;4;4;4;4;5;5] please tell me a general code for this.

回答 (2 件)

Star Strider
Star Strider 2014 年 6 月 23 日
One way of doing it:
V1 = [1; 1; 1; 1];
for k1 = 2:5
if mod(k1,2) == 1
V1 = [V1; [1 1]'*k1];
else
V1 = [V1; ones(6,1)*k1];
end
end
The V1 vector is the result.

Roger Stafford
Roger Stafford 2014 年 6 月 23 日
Or perhaps you would like a probabilistic method:
n = 20; % Length of desired vector
p = 1/4; % Probability of transition
v = cumsum([true;rand(n-1,1)<=p]);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by