Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Probability in printing text

조회 수: 1 (최근 30일)
YM
YM 2019년 5월 16일
마감: MATLAB Answer Bot 2021년 8월 20일
I want, upon starting my function, for one of 5 text statements to be printed. There should be 1/5 chance of any one appearing, and the text printed should generally change with each run. How could I go about doing this? Additionally, if I wanted to change the text statements to choose from (7 instead of 5), how would I do that?

답변 (1개)

Guillaume
Guillaume 2019년 5월 16일
Use randi to choose an integer between 1 and the number of strings, use that to index into a cell array of char vector or a string array:
texts = ["some text", "some other text", "and more", "etc."];
selectedtext = randi(numel(texts));
disp(texts(selectedtext))

이 질문은 마감되었습니다.

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by