MATLAB Encode and Decode fix
Show older comments
A is string input and B is key input(incremented). The first if statement in my function works fine since I'm suppose to keep my encoding within the A-Z characters of ASCII and the key can be from 1-15. But my question is how do I get my 2nd if statement, if the string is larger than the key to repeat like the example above. In the example above, once the key reached 1 at character 'e' it went back to the key of 2 and so on. Can someone help me get the coding for my function to do the same and repeat the key when it's larger than the key. Thanks
6 Comments
Walter Roberson
on 24 Nov 2012
Matt, put your cursor in the box and press the Code button...
per isakson
on 24 Nov 2012
Edited: per isakson
on 24 Nov 2012
@Matt, "if true/false" is the old way to (un)comment multiple lines.
Matt Fig
on 24 Nov 2012
I think you nailed it Walter! I never could have guessed that, haha!
per, that explanation seems plausible too - though I have never used that awful construct.
Walter Roberson
on 2 Dec 2012
Kevin, I see that you edited your question, but it is not obvious to me what was changed?
Answers (2)
Walter Roberson
on 23 Nov 2012
encoded = char(A(k) + 1 + mod(k,2));
17 Comments
Walter Roberson
on 24 Nov 2012
char() and mod() are built into MATLAB.
Whether you want a user-defined function is up to your preferences and any imposed requirements. You can write the functionality as an anonymous function like this:
encode = @(A) char(A + 1 + mod(1:length(A),2))
Walter Roberson
on 24 Nov 2012
'f' plus something is not going to equal 'a' unless that something is negative. Please reconsider why you thing 'deaf' should encode to 'ffca'.
Also, please define what you want 'z' to encode as.
Jim
on 24 Nov 2012
I think this might clarify what Kevin is talking about:
Use functions where appropriate. The program will read in a string and a key. The key is a number from 1 to 15. The allowed characters for the input string and the encrypted string are only the upper case (A-Z). To encrypt, replace the first character by adding the key to it to get the replacement. Increment the key and add that to the second character to get the second encrypted character. Increment the key again and add that to the third, etc. The key rolls over, that is, the key value after 15 is 1. To decrypt take the first character and subtract the original key. Increment the key and subtract that to get the second decrypted character, etc. The main program will input and process four different strings and keys. For each string it will call the encrypt function, passing in the string and the key. This function will pass back the encrypted string to be printed. Then the main will call the decrypt function passing in the string and the original key. The function will pass back the decrypted string to be printed.
Walter Roberson
on 24 Nov 2012
That is somewhat clearer. My code already almost does that.
encode = @(A) char(A + 1 + mod(originalKey + (0:length(A)-1),15))
However, the problem statement does not define what to do if the result of adding the key exceeds 'Z'.
Jim
on 24 Nov 2012
Can you explain what you mean by exceeds Z? Also can you post the previous lines of code because im not sure what some of those variables stand for.
Walter Roberson
on 24 Nov 2012
"A" is the string to be encrypted.
Suppose the string to be encrypted has an 'X' and the key at that point happens to be 2. Add 1 to 'X' and that becomes 'Z'. Add another to that and the 'Z' becomes what? The problem statement does not define what happens if you "add" past 'Z'; it just defines that the output must be A-Z.
Jim
on 24 Nov 2012
I think the safest thing to assume is that Z will roll over to A. I believe that it can be done but im trying my best to find out. Your help with this program is greatly appreciated.
Jim
on 24 Nov 2012
Also I am trying to set up the program so that it encrypts with a function called Encrypt and decrypts with a function called Decrypt . The two functions should be pretty simmilar right? except Encrypt adds and Decrypt subtracts?
Walter Roberson
on 24 Nov 2012
AZ = 'a':'z';
encrypt = @(A, OriginalKey) toupper(AZ(1+mod(A+OriginalKey+mod(0:length(A)-1,15)-65,length(AZ))))
Jim
on 24 Nov 2012
two questions: what does the @ sign do? and does this only encrypt? or does it encrypt and decrypt in one operation?
Walter Roberson
on 24 Nov 2012
Edited: Walter Roberson
on 24 Nov 2012
@ introduces an anonymous function.
The function only encrypts. The change to decrypt is fairly small.
I would not recommend using this code in practice; you won't be able to explain parts of it.
Walter Roberson
on 28 Nov 2012
I still don't understand why you think that f + 1 should be a ??
To get wrap-around, use mod(). Have another look at the code I provided, above.
Walter Roberson
on 28 Nov 2012
Your original question specified that the encoded output should be in A-Z. I asked you more than once before why you thought 'f' should wrap to 'a'. Sigh.
To encode to 'A' to 'E', use
'A' + mod(ShiftedCharacter - 1, 5)
where ShiftedCharacter is the original character plus the appropriate key.
Walter Roberson
on 28 Nov 2012
Use the mod() that I just showed.
Walter Roberson
on 28 Nov 2012
newkey = 1 + mod(currentkey,MaximumKeyValue)
Walter Roberson
on 28 Nov 2012
for k = 1 : length(A)
x(k) = 1 + mod(InitialKey + k - 2, MaximumKeyValue);
end
Jim
on 23 Nov 2012
0 votes
I am having the exact same problem. My question to you Kevin is, is this a function that your main program calls or is this part of your main program? What is the question where 'true' is the answer so the loop can be activated?
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!