# How do I add words onto strings? (Original string is blank but words are added if certain numbers from 1-20 are divisible by 2, 3, 5, or neither. At the end, each number is supposed to have a name)

2 views (last 30 days)
Kyle Donk on 7 Jan 2020
Edited: Stephen Cobeldick on 8 Jan 2020
Original string is blank but words are added if certain numbers from 1-20 are divisible by 2, 3, 5, or neither. At the end, each number is supposed to have a name)
Here is the original question:
Write a cell that displays the “name” of each number from 1 to 20 according to the rules below:
• If the number is divisible by 2, the syllable is “na”
• If the number is divisible by 3, the syllable is “ba”
• If the number is divisible by 5, the syllable is “mo”
• If the number is not divisible by 2, 3, or 5, the number is called “gano”.
These rules should be checked in order and numbers can have more than one syllable. You can use the command mod to check divisibility. If mod(x,y)is 0, then x is divisible by y. For our problem, as an example, the number 10 would be “namo”. The name must be on a single output line. Hint: You will need to use a for loop to iterate through the numbers 1 to 20. Then you will use if statements to test for each syllable.
I am also supposed to put && in between each condition but I do not know how to do this. Everything that I've read has not been overly helpful. Please help!

Show 1 older comment
Kyle Donk on 7 Jan 2020
This is what I have so far:
for x=1:1:20;
A=mod(x,2)
B=mod(x,3)
C=mod(x,5)
name=[];
if mod(x,2)==0
name=[name,na];
A=mod(x,2)
A && B
end
if mod(x,3)==0
name=[name,ba];
end
B && C
if mod(x,5)==0
name=[name,mo];
end
if mod(x,2)~=0,mod(x,3)~=0,mod(x,5)~=0,
name=[name,'gamo'];
end
end
I replaced the strings with brackets because I saw a video where that worked out, but so far, I'm not having any luck.
Walter Roberson on 8 Jan 2020
name=string([]);
if mod(x,2)==0
name=[name,"na"];
A=mod(x,2)
A && B
end
Stephen Cobeldick on 8 Jan 2020
"Hint: ...Then you will use if statements to test for each syllable."
Huh. Or you can use MATLAB arrays and anonymous functions:
>> C = {'na','ba','mo','gano'};
>> G = @(x)[x,~any(x)];
>> F = @(n)[C{G(mod(n,[2,3,5])==0)}];
>> F(10)
ans =
namo

David Hill on 8 Jan 2020
name=cell(1,20);
for x=1:20
if mod(x,2)==0
name{x}='na';
end
if mod(x,3)==0
name{x}=[name{x},'ba'];
end
if mod(x,5)==0
name{x}=[name{x},'mo'];
end
if mod(x,2)~=0&&mod(x,3)~=0&&mod(x,5)~=0
name{x}='gamo';
end
end
display(name);

Kyle Donk on 8 Jan 2020
Whoops! I changed them to curly brackets but I still got an error in Matlab.
Kyle Donk on 8 Jan 2020
My bad, I mispoke. I ran the code with curly braces and Matlab said that curly braces were not supported in this type of problem. I also ran the code with parentheses and it said that "gamo" was not defined.
Stephen Cobeldick on 8 Jan 2020
"I also ran the code with parentheses and it said that "gamo" was not defined. "
Apparently you also removed the leading single quote.
I just tried David Hill's answer and it worked without error.