How to make a column vector of 2 distinct numbers

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by