Position of IF loop

2 次查看(过去 30 天)
Nil
Nil 2011-11-17
Hi All,
Sorry for asking question on such a long code..but i am not getting where to place following "if" loop in the code
spch=spch+1;
if spch==4
spch=1;
end
There are three input/output array for digits/special chars to get cipher text. For 1st digit/special char in input_text, relevant cipher text should be picked up from X output array using z input array like wise for 2nd digit/special char in input_text, relevant cipher text should be picked up from W output array using Y input array...
But I am getting following cipher text
input_text=
professorkhuranawillarriveat10.30p.m.
cipher_text=
nmeazjplhfclovdgoihhvomivzvs~5]-5n}c]
Correct Cihper_Text=
nmeazjplhfclovdgoihhvomivzvs65}&5g}c]
I guess what happening here above if loop getting executed before coming of special char in input_text so "spch" variable getting value as 2 and even for 1st digit/special char in input_text, relevant cipher text getting picked up from W output array using Y input array which wrong
Code--
Code removed
In summary
1) Where to place following if loops for always correct working
spch=spch+1;
if spch==4
spch=1;
end
ch=ch+1;
if ch==4
ch=1;
end
Thanks in Advance

回答(1 个)

Walter Roberson
Walter Roberson 2011-11-17
No need to use the "if" at all:
spch = mod(spch,4) + 1;
By the way,
foundIndex = find(ismember(S, txt(j))==1);
can be rewritten as
[tf, foundIndex] = ismember(txt(j), S);
However, considering the way you are using it, you would be better off switching to character arrays than cell arrays, such as initializing at the top, before the loop:
S = cell2mat( {'a' 'b' 'c' 'd' 'e';
'f' 'g' 'h' 'i' 'j';
'k' 'l' 'm' 'n' 'o';
'p' 'r' 's' 't' 'u';
'v' 'w' 'x' 'y' 'z'} );
K = cell2mat( {'g' 'm' 'r' 'i' 't';
'a' 'b' 'c' 'd' 'e';
'f' 'h' 'j' 'k' 'l';
'n' 'o' 'p' 's' 'u';
'v' 'w' 'x' 'y' 'z'} );
Then the code in that section reduces down to
Q(j) = K(S==j(i));
  1 个评论
Walter Roberson
Walter Roberson 2011-11-27
I note you updated the question and removed your code. Your question of where to put those particular statements is not meaningful without the code. If you are still looking for an answer, then please post the current version of your code.
I do recommend the mod() approach rather than the "if" approach.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by