Can I use randi and say random number from 1 to 30 except 8 and 9? (for example)
78 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Akana Juliet
el 18 de Jun. de 2021
Comentada: Akana Juliet
el 21 de Jun. de 2021
Hi everyone. I have some bad data that I have to exclude from my random index selecter but I can't remove it from my data.
For example, I wanted to say Random integer from 1 to 30, but exclude 8 and 9
This works but when I try to add another integer, like 8 and 9 for example, it doesn't work.
randRow = randi(30,1);
while randRow == 8
randRow = randi(30,1);
end
Do you guys know the right syntax? or is there an easier way to do it?
0 comentarios
Respuesta aceptada
Stephen23
el 18 de Jun. de 2021
Editada: Stephen23
el 18 de Jun. de 2021
This is MATLAB, so your first thought should always be to use arrays and indexing:
vec = setdiff(1:30,8:9) % or [1:7,10:30] or whatever
val = vec(randi(numel(vec)))
3 comentarios
Stephen23
el 21 de Jun. de 2021
vec = setdiff(1:30,[8,9,11,15,18])
How to generate and concatenate vectors is explained here
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices 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!