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

42 ビュー (過去 30 日間)
Wasi von Deutschland
Wasi von Deutschland 2017 年 5 月 27 日
コメント済み: SULE SAHIN 2017 年 11 月 7 日
REMAINING QUESTION: 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 You can see code below. Do you know how one can add number 7 'PQRS' and 9 'WXYZ' and 8 'TUV'. THANKS
  3 件のコメント
Wasi von Deutschland
Wasi von Deutschland 2017 年 5 月 27 日
Stephen I know but I have problem solving it that's why I need your help, feedback and of course recommendations.


回答 (3 件)

Stephen23 2017 年 5 月 30 日
編集済み: Stephen23 2017 年 6 月 3 日
A much simpler solution (based on my comment to an earlier question):
function out = dial(inp)
vec = '012345678922233344455566677778889999';
[~,idb] = ismember(inp,dig);
out = sscanf(vec(idb),'%lu');
(to which it is easy to add input checks), and tested:
>> dial('1FUNDOG4YOU')
ans =
  14 件のコメント
Deepak Sharma
Deepak Sharma 2017 年 9 月 11 日
@Stephen Cobeldick: Thanks, this was a very elegent solution.


J Philps
J Philps 2017 年 5 月 30 日
編集済み: Walter Roberson 2017 年 6 月 3 日
Here is a sample:
function asNumbers = convertPhoneNum(asLetters)
% Make a cell array where each cell represents the letters for a number on the keypad.
lettersByIndex = {'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ'};
% We will build up this final string as the output.
asNumbers = ''; % Preallocate enough space to put the answer in.
% Iterate through the input
for i=1:length(asLetters)
% Check if this character is already a number.
if isstrprop(asLetters(i), 'digit')
asNumbers(i) = asLetters(i);
% If this character is not a number, find the corresponding
% alphabetical representation in the cell array.
% Sample Code:
for j=1:length(lettersByIndex)
% Check if the character can be found in that cell
if ~isempty(strfind(lettersByIndex{j}, asLetters(i))) % If letter is found in that group of letters
asNumbers(i) = num2str(j + 1);

Deepak Sharma
Deepak Sharma 2017 年 9 月 11 日
@Stephen, Thanks for your answer. That's a very simple and beautiful solution. I am very new to Matlab (a few weeks) and I had solved the problem in a cumbersome way. You solution is very elegant.
@Wasi von Deutschland I think the answer would be a bit late for you, but might help someone in future. The answer (edited on 3rd June) by Stephen is almost 99% there. From the questions, there is only one thing remaining that needs to be handled: If there is any special character, return 0. Note that this will be uint64 (and not just say out = 0)
% Check if there are any invalid characters. If yes, return 0.
if sum(~ismember(inp,dig))>0
out = uint64(0);
Once you plug it in, it should work. Hope this helps.
  5 件のコメント
SULE SAHIN 2017 年 11 月 7 日
@Stephen Cobeldick I do anything, ı suppose that I will not solve this question Thank you



Community Treasure Hunt

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

Start Hunting!

Translated by