MATLAB Answers

How do I get four different answers guaranteed generated randomly from several arrays?

2 ビュー (過去 30 日間)
Colin TaylorMays
Colin TaylorMays 2021 年 8 月 17 日
コメント済み: Colin TaylorMays 2021 年 8 月 18 日
I'm a beginner and coding for a personal project to add a little automation to my life. Right now I'm stuck on receiving 4 different answers in two different arrays. I have two matrices of different sizes, one is for lunches and the other has dinners and they share a few meals between them. I want to randomly choose 2 lunches and 2 dinners without any repeats. How could I make sure to generate 4 different meals?
This is the lunch and dinner matrix, you'll see they share a few items.
This is my current code, which randomly generates 4 meals, but sometimes there is a repeat of a meal, which I don't want.


Jeff Miller
Jeff Miller 2021 年 8 月 18 日
One way is to put the two random selections inside a while loop, check whether any meals are repeated, and then only exit the loop when there are no repeats.
  1 件のコメント
Colin TaylorMays
Colin TaylorMays 2021 年 8 月 18 日
That's exactly what I did! Thanks for the answer!


その他の回答 (0 件)




Community Treasure Hunt

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

Start Hunting!

Translated by