obtaining random data in cell array
古いコメントを表示
in the attachment i have imported an excel data sheet containing a column of 100 english nouns. i then imported the data interactively and can be found in the workspace as a cell array with the value of 100x1, named 'nouns'. i am just wondering how i can generate a random integer between 1 and 100 that can choose any one of these words from the data (in short, randomly picking any word from the list of nouns in the workspace).
eventually the objective is to create a hangman game displayed in the image

thanks!
2 件のコメント
Not enough information. What does "i have a 100x1 cell made up of 100 words in each cell" really mean? Are the words stored in a string, or a char vector as natural language sentences, or a char matrix with one word per row padded with spaces, or a cell array of individual words, or something else?
Rather than trying to explain how the data is arranged, it would be much simpler if you simply uploaded the data for us to look at. Or something that is similar enough and has the same features as your data.
Simply edit your question and click the paperclip button to upload the data.
Brigham Tukukino
2017 年 10 月 27 日
回答 (3 件)
KSSV
2017 年 10 月 26 日
randperm(100,1)
Jan
2017 年 10 月 26 日
randi([1,100], 1)
or simpler
randi(100)
Image Analyst
2017 年 10 月 27 日
Try this, which is robust as to the number of words in your workbook
% Read words from the Excel file.
[~, words] = xlsread(xlfileName);
% Get the index of a random word to use. Does not assume 100 - can be any length!
randomIndex = randi(size(words, 1));
% Get the actual word (extract it into a string from the cell).
wordToUse = strings{randomIndex};
カテゴリ
ヘルプ センター および File Exchange で Text Data Preparation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!