How to make a column vector of 2 distinct numbers

1 回表示 (過去 30 日間)
Dubstep Dublin
Dubstep Dublin 2015 年 3 月 19 日
編集済み: Konstantinos Sofos 2015 年 3 月 19 日
How can I make a 400x1 column vector in the following format..
first 100 rows = all 1's
101st to 400th row = all 2's
I am a matlab newbie so any help to get me started will be greatly appreciated.
Thanks

採用された回答

Konstantinos Sofos
Konstantinos Sofos 2015 年 3 月 19 日
編集済み: Konstantinos Sofos 2015 年 3 月 19 日
Many possible solutions
V = [ones(100,1);2*ones(300,1)];
or
V = [repmat(1,100,1);repmat(2,300,1)];
or
V = ones(400,1); V(101:end)=2;
or
V(1:100,1)=1;V(101:400,1)=2;

その他の回答 (3 件)

the cyclist
the cyclist 2015 年 3 月 19 日
One way:
x = ones(400,1);
x(101:400) = 2;
  1 件のコメント
Dubstep Dublin
Dubstep Dublin 2015 年 3 月 19 日
Thank you for your reply. Is there any way of implementing it without the use of 'ones' function such that I can make such an array of different numbers (suppose say 172 instead of 1 and 272 instead of 2)

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


the cyclist
the cyclist 2015 年 3 月 19 日
One way:
x = [repmat(1,[100 1]); repmat(2,[300 1])];

James Tursa
James Tursa 2015 年 3 月 19 日
[ones(100,1);2*ones(300,1)]

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by