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...
Walter Roberson
Walter Roberson 2012년 11월 24일
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.
Matt Fig
Matt Fig 2012년 11월 24일
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.
Walter Roberson
Walter Roberson 2012년 12월 2일
Kevin, I see that you edited your question, but it is not obvious to me what was changed?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2012년 11월 23일

0 개 추천

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))
Walter Roberson
Walter Roberson 2012년 11월 24일
'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
Jim 2012년 11월 24일
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'.
Jim
Jim 2012년 11월 24일
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
Walter Roberson 2012년 11월 24일
"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
Jim 2012년 11월 24일
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
Jim 2012년 11월 24일
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))))
Jim
Jim 2012년 11월 24일
two questions: what does the @ sign do? and does this only encrypt? or does it encrypt and decrypt in one operation?
Walter Roberson
Walter Roberson 2012년 11월 24일
편집: Walter Roberson 2012년 11월 24일
@ 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
Walter Roberson 2012년 11월 28일
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.
Walter Roberson
Walter Roberson 2012년 11월 28일
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