Converting double and integer value to logical array

12 visualizaciones (últimos 30 días)
Matuno
Matuno el 7 de Dic. de 2013
Comentada: Walter Roberson el 9 de Dic. de 2013
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 comentario
Matuno
Matuno el 9 de Dic. de 2013
Could anyone please help me on this problem as I can not find anyway to solve this for many days.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Dic. de 2013
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 comentarios
Matuno
Matuno el 9 de Dic. de 2013
Editada: Matuno el 9 de Dic. de 2013
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

Iniciar sesión para comentar.

Más respuestas (1)

chitresh
chitresh el 7 de Dic. de 2013
y = im2bw(x)
try this it convert it to logical class
if problem is solved accept the answer
  1 comentario
Matuno
Matuno el 7 de Dic. de 2013
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by