Writing a function to count the number of times a word pops up in a random word generation.
3 views (last 30 days)
Show older comments
I'm trying to write a function that
- Generates iterations times a random word of length size;
- Check for each of them whether the input name appears in the generated random word, using the function I made - mychartrec;
- Counts the number of successes, i.e., the number of times the answer is YES in the previous experiment.
My function mychartrec is
function [result] = mychartrec(x, y)
n = length(x);
m = length(y);
if n > m
[result] = false;
return
else
for k = 1:m-n+1
if mychartest(x, y(k:k+n-1))
[result] = true;
return
end
end
end
end
and what I've got for the function I'm trying to create is
function result = iamrandom(size, iterations, name)
result = 0;
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
r = randi(26, iterations, size);
words = alphabet(r);
disp(words)
for i = 1:iterations
word = words(i);
if mychartrec(name, word) == true
result = result + 1;
disp(word)
end
end
end
but whenever I run the function, it always produces a result of 0, even if the word generated includes the name within. I'm trying to do this without using strings.
2 Comments
Rik
on 5 Jun 2022
You're only selecting a single letter. If you use the debugger to step through the code, you can see the state of all variables at each step.
I would also suggest using cellstr. It will allow you to use ismember, without sacrificing any compatibility.
Answers (1)
Voss
on 5 Jun 2022
Edited: Voss
on 5 Jun 2022
Consider the difference between the following two pieces of code.
First, set up the variables for both:
number_of_words = 5;
word_length = 7;
alphabet = 'A':'Z';
r = randi(numel(alphabet), number_of_words, word_length);
words = alphabet(r);
disp(words)
1) Using words(i):
for i = 1:number_of_words
word = words(i)
end
2) Using words(i,:):
for i = 1:number_of_words
word = words(i,:)
end
words is a 2d character array, and words(i) is the ith element of words - a single character, just as the ith element of a numeric matrix is a single number:
A = magic(3)
A(2)
Alternatively, words(i,:) gives you the ith row of words, just as A(i,:) gives you the ith row of the numeric matrix A:
A(2,:)
3 Comments
Voss
on 5 Jun 2022
In mychartrec, reproduced here
function [result] = mychartrec(x, y)
n = length(x);
m = length(y);
if n > m
[result] = false;
return
else
for k = 1:m-n+1
if mychartest(x, y(k:k+n-1))
[result] = true;
return
end
end
end
end
if n <= m (so that the else block executes) but mychartest(x, y(k:k+n-1)) is never true for any k, then the variable result is not defined. That is, result is only defined when there is a match (in which case it is true) or when n > m (in which case there can be no match and it is false). It remains undefined in the case that there could be a match according to the word lengths but there is no match in fact.
You have to make sure result is defined in every possible case, so something like this would work:
function result = mychartrec(x, y)
n = length(x);
m = length(y);
if n > m
result = false;
return
else
for k = 1:m-n+1
if mychartest(x, y(k:k+n-1))
result = true;
return
end
end
% if we're here, then the k loop ran and didn't find any match,
% so result is false
result = false;
end
end
Voss
on 5 Jun 2022
Edited: Voss
on 5 Jun 2022
However, I would simplify that function a bit. Recognizing that a for loop given an empty array executes zero times, you don't have to have a special case for when n > m. You can merely initialize result to be false, then if you find a match, set result to true and return:
function result = mychartrec(x, y)
n = length(x);
m = length(y);
result = false;
for k = 1:m-n+1
if mychartest(x, y(k:k+n-1))
result = true;
return
end
end
end
Note that, in case n > m, the loop executes zero times, and result remains false as it should be.
See Also
Categories
Find more on Loops and Conditional Statements 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!