MATLAB Encode and Decode fix

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 个评论

Matt Fig
Matt Fig 2012-11-23
编辑:Matt Fig 2012-11-23
Why do you have "if true" in there?
Is there some textbook out there that teaches people to do that? I have seen that many times here in the last few months...
Matt, put your cursor in the box and press the Code button...
per isakson
per isakson 2012-11-24
编辑:per isakson 2012-11-24
@Matt, "if true/false" is the old way to (un)comment multiple lines.
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.
Matt Fig
Matt Fig 2012-11-24
编辑:Matt Fig 2012-11-24
Kevin, no worries. If you simply paste your code, highlight it, then hit the code button that 'if true' won't appear...
Remember to offset the lines of code by an empty line above and below.
Kevin, I see that you edited your question, but it is not obvious to me what was changed?

请先登录,再进行评论。

回答(2 个)

encoded = char(A(k) + 1 + mod(k,2));

17 个评论

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))
'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.
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.
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'.
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.
"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.
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.
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?
AZ = 'a':'z';
encrypt = @(A, OriginalKey) toupper(AZ(1+mod(A+OriginalKey+mod(0:length(A)-1,15)-65,length(AZ))))
two questions: what does the @ sign do? and does this only encrypt? or does it encrypt and decrypt in one operation?
@ 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.
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.
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.
Kevin
Kevin 2012-11-28
编辑:Kevin 2012-11-28
it rolls over, encoding can only be in a range of characters a-f in the example. so f+1-> g but that's unacceptable for the given range a-f only. So adding 1 to f makes it go back to a, which is an acceptable range for the permitted range of characters. It's there is given range that is only permitted for this function, it's meant to do that for this function basically.
Use the mod() that I just showed.
newkey = 1 + mod(currentkey,MaximumKeyValue)
for k = 1 : length(A)
x(k) = 1 + mod(InitialKey + k - 2, MaximumKeyValue);
end

请先登录,再进行评论。

Jim
Jim 2012-11-23

0 个投票

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?

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

产品

提问:

2012-11-23

Community Treasure Hunt

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

Start Hunting!

Translated by