Error using countOnes = sum(num); when trying to count how many zeros and ones are randomly generated.
I have never used MATLAB before, and am trying to randomly generate 0s and 1s to calculate the probability of getting heads or tails on a coin. I used the code below, but keep getting an error on the line showing countOnes = sum(num); saying ‘array indices must be positive integers or logical values.’. Anyone know what might be wrong? I clicked the explain error and it wasn’t clear.
rng(‘shuffle’); %Generates a random number each time, instead of the same sequence
N = 1000; %Sets value of N to 1000 to simulate 1000 flips of the coin
num = randi([0,1], N, 1); %Generates a number 0 or 1
countOnes = sum(num);
countZeros = N – countOnes;
disp([‘Number of 1s: ‘, num2str(countOnes)]);
disp([‘Number of 0s: ‘, num2str(countZeros)]);I have never used MATLAB before, and am trying to randomly generate 0s and 1s to calculate the probability of getting heads or tails on a coin. I used the code below, but keep getting an error on the line showing countOnes = sum(num); saying ‘array indices must be positive integers or logical values.’. Anyone know what might be wrong? I clicked the explain error and it wasn’t clear.
rng(‘shuffle’); %Generates a random number each time, instead of the same sequence
N = 1000; %Sets value of N to 1000 to simulate 1000 flips of the coin
num = randi([0,1], N, 1); %Generates a number 0 or 1
countOnes = sum(num);
countZeros = N – countOnes;
disp([‘Number of 1s: ‘, num2str(countOnes)]);
disp([‘Number of 0s: ‘, num2str(countZeros)]); I have never used MATLAB before, and am trying to randomly generate 0s and 1s to calculate the probability of getting heads or tails on a coin. I used the code below, but keep getting an error on the line showing countOnes = sum(num); saying ‘array indices must be positive integers or logical values.’. Anyone know what might be wrong? I clicked the explain error and it wasn’t clear.
rng(‘shuffle’); %Generates a random number each time, instead of the same sequence
N = 1000; %Sets value of N to 1000 to simulate 1000 flips of the coin
num = randi([0,1], N, 1); %Generates a number 0 or 1
countOnes = sum(num);
countZeros = N – countOnes;
disp([‘Number of 1s: ‘, num2str(countOnes)]);
disp([‘Number of 0s: ‘, num2str(countZeros)]); random number generator, probability MATLAB Answers — New Questions