How to make pairs of 1s and 0s in an array?

6 ビュー (過去 30 日間)
Kevin
Kevin 2022 年 1 月 13 日
コメント済み: Kevin 2022 年 1 月 14 日
close all
clear
clc
m = 5 %amount of 1s
k = 13 %length of array
n = k-m %amount of 0s
rhythm_0 = [repelem(0,n)]
rhythm_1 = [repelem(1,m)]
So now I got 2 arrays, how do I combine them into one array so there are pairs of 01s like with the code above so the array looks like this:
0101010101000
  2 件のコメント
Kevin
Kevin 2022 年 1 月 14 日
The difference however is that I managed to write a little bit of the scrip myself, but I didn't really know how to go further from here.

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

採用された回答

Voss
Voss 2022 年 1 月 13 日
編集済み: Voss 2022 年 1 月 13 日
close all
clear
clc
m = 5 %amount of 1s
m = 5
k = 13 %length of array
k = 13
n = k-m %amount of 0s
n = 8
% rhythm_0 = [repelem(0,n)]
% rhythm_1 = [repelem(1,m)]
if m > n
extra_0 = [];
extra_1 = ones(1,m-n);
p = n;
else
extra_0 = zeros(1,n-m);
extra_1 = [];
p = m;
end
rhythm = [reshape([zeros(1,p); ones(1,p)],1,[]) extra_0 extra_1]
rhythm = 1×13
0 1 0 1 0 1 0 1 0 1 0 0 0
  1 件のコメント
Kevin
Kevin 2022 年 1 月 14 日
Thank you so much, you helped me a lot!

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by