MManassi MManassi - 6 months ago 9
Javascript Question

Repeated sequence of numbers in Javascript

I want to generate a vector of 100 values composed by [1 0]:
This is how I did it in Matlab:

n = 100;
Seq1 = [1 0]; % sequence of 1-0
Vector = repmat(Seq1,(n/2),1); % Creates n/2 sequences of 1-0


The result is a vector like: [1 0 1 0 1 0 1 0...]

Is there a way to get the same result with JavaScript?

Answer

Assuming you're looking for a way to add a 1 and then a 0, not an array containing 1 and 0:

var myArray = [];
nTrials = 30;
for(i = 1; i<= nTrials/2; i++){
  myArray.push(1);
  myArray.push(0)
}
document.body.innerHTML = myArray[1];}

https://jsfiddle.net/6seqs6af/1/