- 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.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (4)
Srishti Saha
el 10 de Mzo. de 2018
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 comentarios
Rik
el 11 de Abr. de 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.
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 comentarios
Mohamed Sirajudeen
el 27 de Ag. de 2017
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
el 27 de Ag. de 2017
Editada: Stephen23
el 27 de Ag. de 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.
Guillaume
el 11 de Abr. de 2017
Editada: Guillaume
el 11 de Abr. de 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
6 comentarios
Guillaume
el 11 de Abr. de 2017
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
el 11 de Abr. de 2017
Or don't convert to a vector with numbers, but to chars (after which you can use str2double).
RAMAKANT SHAKYA
el 7 de Feb. de 2019
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 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!