help with vectors MatLab?

3 ビュー (過去 30 日間)
Bojan
Bojan 2014 年 3 月 28 日
回答済み: Bojan 2014 年 3 月 28 日
Hello!
I have a vector
H =
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
I want it to be like
H= 0 1 0 1 0 1 0 1 0 1 0 1......
I tried with H(:) but it wont work, there is 17 zeros and 16 ones is that maybe a problem? Hope someone can help me . Kind regards

採用された回答

Bojan
Bojan 2014 年 3 月 28 日
H2 = H;
H2([1:2:end,2:2:end])=H;
this is the solution !

その他の回答 (1 件)

Niklas Nylén
Niklas Nylén 2014 年 3 月 28 日
編集済み: Niklas Nylén 2014 年 3 月 28 日
Your question is a bit unclear. If you want to create a vector H containing alternating 0's and 1's, starting and ending with 0 with the mentioned length you can do it like this:
H = [repmat([0 1],1,16) 0]
If you just want to transpose H to a row vector, which will give you H = [0 0 0 ... 1 1 1...] you can do it like this:
H = H'
As far as I know there is no build in function which will give H = [0 1 0 1 ...], for this you need some more code.
H1 = H(1:ceil(length(H)/2)); % first half of H
H2 = [H(ceil(length(H)/2)+1:end); 1]; % second half of H, add a 1 at the end to get equal lengths
H=[H1 H2];
H = H(1:end-1)' % Do not include the final 1
  1 件のコメント
Bojan
Bojan 2014 年 3 月 28 日
Yes, I would like to alternate 0's ant 1's. What if I do not know length of my vector but I know that 0's are coming first and then after them 1's.

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by