Position of IF loop

2 visualizzazioni (ultimi 30 giorni)
Nil
Nil il 17 Nov 2011
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 Commento
Walter Roberson
Walter Roberson il 8 Dic 2011
There is no such thing as an if loop.

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 17 Nov 2011
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 Commento
Walter Roberson
Walter Roberson il 27 Nov 2011
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.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by