How to make pairs of 1s and 0s in an array?
    6 views (last 30 days)
  
       Show older comments
    
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
Accepted Answer
  Voss
      
      
 on 13 Jan 2022
        
      Edited: Voss
      
      
 on 13 Jan 2022
  
      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)]
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]
More Answers (0)
See Also
Categories
				Find more on Entering Commands in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

