How to create a random distribution of 2 words in 30 places?
1 view (last 30 days)
Show older comments
KALPAJYOTI HAZARIKA
on 3 Aug 2019
Answered: Image Analyst
on 4 Aug 2019
I am confused to create the random distribution of the two words such as "mango" and "orange" into 30 places?
0 Comments
Accepted Answer
Walter Roberson
on 3 Aug 2019
Words = ["mango", "orange"] ;
Output = Words(randi(length(Words), 1, 30);
0 Comments
More Answers (1)
Image Analyst
on 4 Aug 2019
Try this:
sentence = [];
for k = 1 : 30
sentence = sprintf('%sword%d ', sentence, k);
end
sentence
replacementWords = {'mango', 'orange'};
% Split apart into separate words
individualWords = strsplit(sentence, ' ') % Space is the delimiter.
% Get two random word locations to replace
indexes1 = randperm(30, 2)
indexes2 = randperm(2, 2);
individualWords{indexes1(1)} = replacementWords{indexes2(1)}
individualWords{indexes1(2)} = replacementWords{indexes2(2)}
% Built up a sentence from the separate words
sentence2 = [];
for k = 1 : 30
sentence2 = sprintf('%s%s ', sentence2, individualWords{k});
end
sentence2
sentence =
'word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12 word13 word14 word15 word16 word17 word18 word19 word20 word21 word22 word23 word24 word25 word26 word27 word28 word29 word30 '
individualWords =
1×31 cell array
Columns 1 through 11
{'word1'} {'word2'} {'word3'} {'word4'} {'word5'} {'word6'} {'word7'} {'word8'} {'word9'} {'word10'} {'word11'}
Columns 12 through 22
{'word12'} {'word13'} {'word14'} {'word15'} {'word16'} {'word17'} {'word18'} {'word19'} {'word20'} {'word21'} {'word22'}
Columns 23 through 31
{'word23'} {'word24'} {'word25'} {'word26'} {'word27'} {'word28'} {'word29'} {'word30'} {0×0 char}
indexes1 =
23 24
individualWords =
1×31 cell array
Columns 1 through 11
{'word1'} {'word2'} {'word3'} {'word4'} {'word5'} {'word6'} {'word7'} {'word8'} {'word9'} {'word10'} {'word11'}
Columns 12 through 22
{'word12'} {'word13'} {'word14'} {'word15'} {'word16'} {'word17'} {'word18'} {'word19'} {'word20'} {'word21'} {'word22'}
Columns 23 through 31
{'orange'} {'mango'} {'word25'} {'word26'} {'word27'} {'word28'} {'word29'} {'word30'} {0×0 char}
sentence2 =
'word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12 word13 word14 word15 word16 word17 word18 word19 word20 word21 word22 orange mango word25 word26 word27 word28 word29 word30 '
>>
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!