Hey Guys can you help me with that.

Hey guys, here is my task.
Each number on telephone keypads, except 0 and 1, corresponds to a set of uppercase letters as shown in this list: 2 ABC, 3 DEF, 4 GHI, 5 JKL, 6 MNO, 7 PQRS, 8 TUV, 9 WXYZ Hence, a phone-number specification can include uppercase letters and digits. Write a function called dial that takes as its input argument a char vector of length 16 or less that includes only these characters and returns as its output argument the telephone number as a uint64. Here is the input and output for one example of a call of the function: Input: '1FUNDOG4YOU' Output: 13863644968 You can assume that a phone number never starts with 0. If the input contains any illegal characters, the function returns 0. You are not allowed to use the built-in function strrep.
I don't want so much of a made code but if u could tell me:
1 How should i write my function so the input can be both numbers and chars ?
2 How to convert this input to many single chars.
3.Do you think my if statement should be something like if p{2} = a b c p{2} = 2
I would also accept a whole code but please explain itas fora begginer programmer. Thank you

3 Comments

  1. The assignment already told you that, and showed you with the example. Read the assignment again.
  2. Don't. That would be a total waste of MATLAB, and a pointlessly complicated way to write code. The name "MATLAB" stands for MATrix LABoratory, and not for "Lets split the data into lots of separate individual variables". Your code will be neater, faster, simpler, and more reliable when you process your data all together in one array, not as lots of separate characters.
  3. No. Why do you need an IF at all? That seems like a bad way to solve this task (see answer 2 for more details).
You seem intent on splitting data and using IFs. While you could make this work, it will be more complicated and harder to write than if you learn how to handles arrays of data. Because that is what MATLAB is for. Much simpler is to work with the whole array at once, something like this:
function out = dial(inp)
dig = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
vec = '012345678922233344455566677778889999'-'0';
[ida,idb] = ismember(inp,dig);
out = uint64(vec(idb));
end
(to which it is easy to add input checks), and tested:
>> dial('1FUNDOG4YOU')
ans =
1 3 8 6 3 6 4 4 9 6 8
I have somehow missed your answer. It took me almost no time to understand it its supper. Thank you

Sign in to comment.

Answers (4)

I think this is the most elegant solution that takes care of all exceptions. Apologies if it has been covered in the past:
function out_dig = dial(inp_string)
characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
digits = '012345678922233344455566677778889999';
if sum(~ismember(inp_string,characters))>0
out_dig = uint64(0);
return;
else
[~,idb] = ismember(inp_string,characters);
out_dig = sscanf(digits(idb),'%lu');
end
end
Rik
Rik on 11 Apr 2017
You don't have to do anything special to the input, as the question states that the input will always be a string. This string is already a vector, so you can used indices to get specific characters from that string.
I would advise you to check out isstrprop.
You can use a technique with ismember to do something similar to your third point, but you can also use the fact that letters have an ASCII value (A is 64, B is 65 etc), which you can then modify with some rounding.
Please let me know if this is explicit enough for you, or if you need some more code. (And don't forget the conversion to uint64 and the edge case of illegal input)

2 Comments

function out =dial(inp) if ~all(ismember(inp,['0':'9','A':'Z'])) length(inp) > 16 out = uint64(0); return; end dig = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; vec = '012345678922233344455566677778889999'-'0'; [ida,idb]=ismember(inp,dig); out =uint64(vec(idb)); end I have tried this... when i give the input for above it works in matlab. but grader shows error, any idea why
Stephen23
Stephen23 on 27 Aug 2017
Edited: Stephen23 on 27 Aug 2017
"it works in matlab. but grader shows error, any idea why"
Because the code that I wrote and you copied solves the core task of converting letters and digits to a telephone number, but does not deal with any of the edge-cases and special cases that your assignment requires. Why did you submit code to get graded if you do not even understand what it is doing, and more importantly, what it is not doing?
It is your assignment, so you have to figure out how to make it work. Read the requirements that you were given: does the code fulfill them all? Did you test them yourself? Think about how you will need to change the code to make it fulfill those requirements, and then test it to make sure that it does.

Sign in to comment.

Guillaume
Guillaume on 11 Apr 2017
Edited: Guillaume on 11 Apr 2017
1. The task clearly says "takes as its input argument a char vector". There is no option for the input to be numeric (however some of the characters could be digits like '1'). In any case, matlab does not care about the types of inputs.
2. You use indexing to get at characters of char arrays, same as you use for numeric matrix:
telchar = '1FUNDOG4YOU';
telchar(5) %get 5th character
3. Your statement is not even valid matlab syntax. You could indeed loop over the characters one at a time, then use a bunch of if ... elseif ... else ... end or switch ... case ... otherwise ... end to convert from characters to numbers, or... you could be clever and use a look up table to do it in only three lines:
str = 'A1BDACB'; %e.g.
lookuptable = nan(1, 68); %create empty lookup table for ascii codes from 1 to 68. 68 is ascii code for 'D'
lookuptable('1ABCD') = [1 2 2 3 3]; fill lookup table with values for '1', 'A', 'B', 'C', 'D'
num = lookuptable(str) %convert input char vector into vector of numbers according to look-up table
Not present in the above is checks that the input is valid and conversion of the vector of numbers into a single number. Look up any and isnan for the former, and simple matrix multiplication or polyval for the latter.

6 Comments

Hmmm. From what I understand shouldn't we creat an ascii code from 48 to 122 because in this interval there are the numbers and letters .
Afther that why do we have [1 2 2 3 3] on the otehr side of lookuptable('1ABCD')
Isn't the Idea of the look up table, that there is a number equal to every letter. Why do we have to fill it up ? Shouldn't this table already be filled up.
Here is what i came up with so far, but I dont know yet, why its not working properlly. How do u think we can put this table in my code.
function out = dial(x)
l = length(x);
for i = 1:l
if x(i) == '1'
w(i) = 1;
elseif x(i) == '2'
w(i) = 2;
elseif x(i) == '3'
w(i) = 3;
elseif x(i) == '4'
w(i) = 4;
elseif x(i) == '5'
w(i) = 5;
elseif x(i) == '6'
w(i) = 6;
elseif x(i) == '7'
w(i) = 7;
elseif x(i) == '8'
w(i) = 8;
elseif x(i) == '9'
w(i) = 9;
elseif x(i) == 'A' || x(i) == 'a' || x(i) == 'B' || x(i) == 'b'|| x(i) == 'C' || x(i) == 'c'
w(i) = 2;
elseif x(i) == 'D' || x(i) == 'd' || x(i) == 'E' || x(i) == 'e'|| x(i) == 'F' || x(i) == 'f'
w(i) = 3;
elseif x(i) == 'G' || x(i) == 'g' || x(i) == 'H' || x(i) == 'h'|| x(i) == 'I' || x(i) == 'i'
w(i) = 4;
elseif x(i) == 'J' || x(i) == 'j' || x(i) == 'K' || x(i) == 'k'|| x(i) == 'L' || x(i) == 'l'
w(i) = 5;
elseif x(i) == 'M' || x(i) == 'm' || x(i) == 'N' || x(i) == 'n'|| x(i) == 'O' || x(i) == 'o'
w(i) = 6;
elseif x(i) == 'P' || x(i) == 'p' || x(i) == 'Q' || x(i) == 'q'|| x(i) == 'R' || x(i) == 'r' || x(i) == 'S' || x(i) == 's'
w(i) = 7;
elseif x(i) == 'T' || x(i) == 't' || x(i) == 'U' || x(i) == 'u'|| x(i) == 'V' || x(i) == 'v'
w(i) = 8;
elseif x(i) == 'W' || x(i) == 'w' || x(i) == 'X' || x(i) == 'x'|| x(i) == 'Y' || x(i) == 'y' || x(i) == 'Z' || x(i) == 'z'
w(i) = 9;
end
end
out = uint64(w);
end
"From what I understand shouldn't we creat an ascii code from 48 to 122". Indeed, this was just an example. As it is homework I didn't want to give a fully fleshed out solution.
"Isn't the Idea of the look up table, that there is a number equal to every letter". Exactly and NaN for invalid letters so you can easily check if a letter is invalid.
"Why do we have to fill it up ? Shouldn't this table already be filled up". You can create a variable already filled up manually if you prefer. In my example that would be:
lookuptable = [nan(1, 48), 1, nan(1, 65-49-1), 2, 2, 3, 3];
I'd rather make the mapping explicit the way I've done it in my example.
As to your code, a few things:
if x(i) == 1
x(i) = 1;
Well, that's a bit pointless. You're replacing a value of 1 with a value of .... 1
In any case, since x is made of characters
if x(i) == 1
check that the character code of x(i) is 1, not '1'. char(1) is a control character.
Rather than
if x(i) == 'A' || x(i) == 'a' || x(i) == 'B'
ismember(x(i), 'AaB')
Finally, you don't want to replace the original elements of the char vector by numbers. You want to put these numbers into a new numerical vector.
Note that the lookup method is still a lot simpler than this endless list of if... elseif ...
@Radoslav Gagov: Why make it so complicated? See my comment to the original question, which shows how this can be simpler, faster, neater, easier to understand, less buggy,...
MATLAB is a high-level language, so you should not use a thousand IFs as if this was some poor low-level language like C.
Can u Guys tell me how i can make my answe a 1x1 matrix not a 1x11 . I tried with the str2num but it allways give errors : Requires character vector or array input.
As written in my original answer: "Not present in the above is [...] conversion of the vector of numbers into a single number. Look up [...] simple matrix multiplication or polyval for the latter."
e.g: conversion of vector in base 2 into decimal number:
v2 = [1 0 0 1];
%using simple matrix multiplication:
sum(v2 .* 2.^(numel(v2)-1:-1:0))
%using polyval:
polyval(v2, 2)
or use a loop.
Or don't convert to a vector with numbers, but to chars (after which you can use str2double).

Sign in to comment.

function s2=dial(n)
p=[n];
s1='';
tx = ismember(p, ['A':'Z','0':'9']);%only digits and capital letters
tx1=sum(tx);
if length(p)<=16&& p(1,1)~='0'&& tx1==length(p) %finding letters
for c=1:length(p)
if p(c)=='A' || p(c)=='B' || p(c)=='C'
s='2';
elseif p(c)=='D' || p(c)=='E' || p(c)=='F'
s='3';
elseif p(c)=='G' || p(c)=='H' || p(c)=='I'
s='4';
elseif p(c)=='J' || p(c)=='K' || p(c)=='L'
s='5';
elseif p(c)=='M' || p(c)=='N' || p(c)=='O'
s='6';
elseif p(c)=='P' || p(c)=='Q' || p(c)=='R' || p(c)=='S'
s='7';
elseif p(c)=='T' || p(c)=='U' || p(c)=='V'
s='8';
elseif p(c)=='W' || p(c)=='X' || p(c)=='Y' || p(c)=='Z'
s='9';
else
for r=0:9 %finding digit if present
t=num2str(r);
if p(c)==t
s=t;
end
end
end
s1=strcat(s1,s); %adding string
s2=str2num(s1); %change into number
s2=uint64(s2); %changing class
end
else
s2=uint64(0);
end
end

Asked:

on 11 Apr 2017

Answered:

on 7 Feb 2019

Community Treasure Hunt

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

Start Hunting!