フィルターのクリア

Converting double and integer value to logical array

8 ビュー (過去 30 日間)
Matuno
Matuno 2013 年 12 月 7 日
コメント済み: Walter Roberson 2013 年 12 月 9 日
I have looked for a clue on converting to logical array but could not find. My code:
function w = funcmake(n,k)
sc = func1(k);%func1 calls one function
l = [];
j0 = 0;
i0 = 0;
for s0 = 1:n
[r, i0, j0, sc]=func2(i0, j0, sc);%func2 calls another function
l = [l r];
w = xor(n, l);%Here I need to convert them into logical array otherwise showing 0
end
I need to convert n and l to logical array first otherwise result shows 0. I have tried with
y = logical(x) but it does not work.
  1 件のコメント
Matuno
Matuno 2013 年 12 月 9 日
Could anyone please help me on this problem as I can not find anyway to solve this for many days.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2013 年 12 月 9 日
In your line
w = xor(n, l)
your "n" is positive number that is at least 1, or else the "for s0 = 1:n" would not have executed any cycles at all.
Any non-zero number is considered to be logically true. xor() of true and something else is going to the logical NOT of the second value -- so it is going to be 0 if the second value is non-zero, and is going to be 1 only if the second value is 0.
I have no idea what you are hoping for.
Perhaps what you are hoping for is
binary_n = dec2bin(n) - '0';
  2 件のコメント
Matuno
Matuno 2013 年 12 月 9 日
編集済み: Matuno 2013 年 12 月 9 日
I am sorry to make the question so obscure thinking of the space my whole code would take. I am very new in Matlab programming, basically I am trying to implement an RC4 encryption for suppose an integer '1234' and key 'hi'. As I am converting it from C program the C program has:data[k]^S[(S[i]+S[j]) %256]; which I think xoring. Here is my full code for your better understanding:
function w = rc4make(n,k)
sc = rc4key(k);
l = [];
j0 = 0;
i0 = 0;
for s0 = 1:n
[r, i0, j0, sc]=rc4out(i0, j0, sc);
l =[l r];
L1 = logical(n);
disp(L1);
L2 = logical(l);% converting into logical array
disp(L2);
w = xor(L1,L2);
end
function sc=rc4key(key)
le = length(key);
sc = 0:255;
j0 = 0;
% scramble the key schedule
for i0 = 0:255
k0 = floor(key( floor(mod(i0,le))+1 ))
j0 = floor(mod( j0 + k0 + sc(i0+1), 256));
tm = sc(i0+1);
sc(i0+1) = sc(j0+1);
sc(j0+1) = tm;
end
function [r, i0, j0, sc]=rc4out(i0, j0, sc)
i0 = mod( (i0+1), 256);
j0 = mod( j0 + sc(i0+1), 256);
tmp = sc(j0+1);
sc(j0+1) = sc(i0+1);
sc(i0+1) = tmp;
r = mod(sc(i0+1) + sc(j0+1), 256);%(S[i]+S[j]) %256
end

サインインしてコメントする。

その他の回答 (1 件)

chitresh
chitresh 2013 年 12 月 7 日
y = im2bw(x)
try this it convert it to logical class
if problem is solved accept the answer
  1 件のコメント
Matuno
Matuno 2013 年 12 月 7 日
Thank you for your answer, but unfortunately this is converting the values in 1 (i.e. here as your example 'y' value displays 1 in worksheet) which makes the xor 0 as I previously said.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by