Converting double and integer value to logical array

11 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by