save value into new variable after each iteration

조회 수: 7 (최근 30일)
Maria Imdad
Maria Imdad 2020년 11월 4일
댓글: J. Alex Lee 2020년 11월 5일
Hi everyone,
I am new to matlab, I have this one fuction, as a subpart of my code.
Here I am taking an initial string of 80 bits and after execution this loop, returns me 31 different strings as sub keys of the same length.
All I want is to save the value of these 31 keys in 31 different variables as "key1", "key2"....."key31".
This is my function:
function key_out=updateKey(key, i)
sbox=[12 5 6 11 9 0 10 13 3 14 15 8 4 7 1 2];
key_out=[key(62:80) key(1:61)];
key_out(1:4)=dec2bin(sbox(bin2dec(key_out(1:4))+1),4);
key_out(61:65)=dec2bin(bitxor(i, bin2dec(key_out(61:65))), 5);
disp(i);
disp('Output key');
disp(key_out);
end
  댓글 수: 6
Mario Malic
Mario Malic 2020년 11월 4일
My mistake, your solution is much better! Why did you use the reversed direction of loop, would it make a difference in the line below?
key_out(61:65)=dec2bin(bitxor(i, bin2dec(key_out(61:65))), 5);
J. Alex Lee
J. Alex Lee 2020년 11월 5일
The reverse loop is just to implicitly preallocate the arrays that are being populated in the loop.
It shouldn't make a difference in the line because you still are indexing consistently.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by