필터 지우기
필터 지우기

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년 12월 8일
There is no such thing as an if loop.

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

답변 (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