The last error message is further in the code but I believe the problem is coming from the if statement because the Bcol2 is coming up as [] in the code when there should be numbers in it. Sometimes it works sometimes it doesn't I just want it to be consistent every time, and ignore the Bcol2, it should Bcol1 but its the same code for finding Bcol1 I'm just getting errors for each col variable and solving one should help solve the other.
Im trying to get a column vector with a list of composite numbers within a random range using the randi function but I keep getting an error.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
My project is creating two BINGO cards that compete with eachother to see who wins first. The columns need to consist of composite numbers within a 20 number range (for B column its composite numbers that are in 1-99 but I must create a column vector to find all composite numbers within a 20 number range within the 1-99 range).
Here is code for B column vector for card 1:
%getting numbers in random range for both card1 B column
randB1 = randi([1 99]);
i = 0;
while i < 1
if (randB1 - 20) < 0
randB1 = randi(length(B));
else
rangeB1 = [randB1-20 randB1]; %checking to make sure first range number isnt <0
i= i +1;
end
end
%organizing range to put in Bcol1
if isprime(rangeB1(1))
rangB1=find(c==rangeB1(1)+1);
else
rangB1=find(c==rangeB1(1));
end
if isprime(rangeB1(2))
rangB2=find(c==rangeB1(2)-1);
else
rangB2=find(c==rangeB1(2));
end
Bcol1=c(rangB1:rangB2)';
randB2 = randi([1 99]);
This is the error I'm getting:
Error using randi
First input must be a positive scalar integer value IMAX, or two integer values [IMIN IMAX] with IMIN less than or equal to IMAX.
Error in Project (line 289)
r2 = randi([1,length(Bcol2)]);
10 comentarios
Walter Roberson
el 2 de Ag. de 2021
temp = sort([rangB1, rangB2])
Bcol1 = c(temp(1):temp(2)).';
Respuestas (1)
Walter Roberson
el 17 de Ag. de 2021
for K = 1:20:99
range = K:K+19;
composites = setdiff(range(~isprime(range)),1); %1 is never prime
composites(randperm(length(composites),5))
end
0 comentarios
Ver también
Categorías
Más información sobre String en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!