How can I write a random function with names?

4 ビュー (過去 30 日間)
Martha Spadaccino
Martha Spadaccino 2019 年 5 月 15 日
コメント済み: Martha Spadaccino 2019 年 5 月 16 日
Hi all. I'm very new to MatLab programming. I have to write a random function that returns 1 pair of colour name. How do I do?
  2 件のコメント
Rik
Rik 2019 年 5 月 15 日
Have a read here and here. It will greatly improve your chances of getting an answer. Also: if you're new, you will probably benefit from doing a Matlab tutorial, like e.g. this one.
Martha Spadaccino
Martha Spadaccino 2019 年 5 月 16 日
Thank you very much! I have already seen these topics before write my question and I didn't find anything.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2019 年 5 月 16 日
First off, put together the data from https://blog.xkcd.com/2010/05/03/color-survey-results/ and https://en.wikipedia.org/wiki/List_of_colors:_A%E2%80%93F and https://digitalsynopsis.com/design/color-thesaurus-correct-names-of-shades/ and other sites, all to create a large list of color names. I suggest converting it all to lowercase.
Then put together some code that, for every call, creates a random vector of characters that are valid in MATLAB statements (not much point in permitting anthing outside of space (char(32)) to ~ (char(126)).
Then loop. At each step, generate a character vector, add '@()' in front of it, and use str2func() on that. What that gives you is a function handle. Then try/catch/end on executing the function handle with a single output and assigning the result to a variable. If the result is a character vector or is a string object, then convert to lowercase and ismember() it against the large list of color names. If you get a match, then you have found your random function that returns a color name.
... this might take rather a lot of executions. Shorter color names are more probable, but it is certainly not impossible that the function will happen to generate 'ballet slipper' on its second run instead of several tens of thousand of runs before it accidentally emits 'red'
  2 件のコメント
Stephen23
Stephen23 2019 年 5 月 16 日
"First off, put together the data from ... and other sites..."
You can easily save yourself the trouble of doing that (I've already done it for you):
Martha Spadaccino
Martha Spadaccino 2019 年 5 月 16 日
Thank you so much for the answer. I do create a series of opponents colors, so I do choice only few color and have as output a pairs of then without have the same colors in pair. How can I do?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by