# How to generate a rhythm in Matlab?

2 visualizzazioni (ultimi 30 giorni)
Kevin il 15 Set 2021
Risposto: KSSV il 15 Set 2021
I want to generate a rhythm in Matlab that consists of 1s and 0s, but I don't know how I should go about it.
So I know that I have a variable n = the amount of 1s and k = the length
So for (n,k) = (2,4) it could be 0 1 1 0
Any help would be appreciated.
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Risposta accettata

KSSV il 15 Set 2021
n = 2 ; % number of 0's
l = 4 ; % length of array
m = l-n ; % number of 1's
iwant = [repelem(0,n) repelem(1,m)] ;
% randomise the indices
idx = randperm(l) ;
iwant = iwant(idx)
iwant = 1×4
0 1 0 1
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su MATLAB Coder in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!