- The assignment already told you that, and showed you with the example. Read the assignment again.
- 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.
- 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).
Hey Guys can you help me with that.
2 次查看(过去 30 天)
显示 更早的评论
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
回答(4 个)
Srishti Saha
2018-3-10
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
0 个评论
Rik
2017-4-11
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.
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 个评论
Mohamed Sirajudeen
2017-8-27
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
2017-8-27
编辑:Stephen23
2017-8-27
"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.
Guillaume
2017-4-11
编辑:Guillaume
2017-4-11
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
6 个评论
Guillaume
2017-4-11
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.
Rik
2017-4-11
Or don't convert to a vector with numbers, but to chars (after which you can use str2double).
RAMAKANT SHAKYA
2019-2-7
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!