Converting double and integer value to logical array

조회 수: 11 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by