save value into new variable after each iteration

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

This is a bad idea. wouldn't it rather be better for you to have a single variable that contains all the keys, where each row would be a single key? I modified your code and hope it works, as I can't test it. Also, it's best to replace ii with another dummy variable, like ii or any other letter(s).
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(i,:)=[key(i,62:80) key(i,1:61)];
key_out(i,1:4)=dec2bin(sbox(bin2dec(key_out(i,1:4))+1),4);
key_out(i,61:65)=dec2bin(bitxor(i, bin2dec(key_out(i,61:65))), 5);
disp(i);
disp('Output key');
disp(key_out(i,:));
end
Maria Imdad
Maria Imdad 2020년 11월 4일
편집: Maria Imdad 2020년 11월 4일
i got this error on line number 3 "Index in position 1 exceeds array bounds (must not exceed 1)."
'All I want is to save the value of these 31 keys in 31 different variables as "key1", "key2"....."key31".'
Numbering variables is a sign that you are doing something wrong.
Forcing meta-data (e.g. pseudo-indices) into variable names is a sign that you are doing something wrong.
The simple and very efficient alternative is to use indexing. You should use indexing.
Mario, shouldn't his function's only responsibility be to transform the "key" (a character array, not a string) based on the input i, and not have to worry about what's being done with the output? That decision can be made outside of the function.
Maria, Mario's suggestion assumes that you have some code that is using your function that is attempting to do what you described (serially name variables, which I agree with Mario is a VERY BAD idea).
To achieve Mario's suggestion without changing your updateKey function, you can define the character matrix variantkeysChAr as below. Or you can store it in a cell array, which I think would be easier and more generalizable
baseKey = sprintf('%d',randi(2,1,80)-1)
NumVariants = 31;
for i = NumVariants:-1:1
variantkeysCell{i,1} = updateKey(baseKey, i);
variantkeysChAr(i,:) = updateKey(baseKey, i);
end
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
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);
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개)

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

질문:

2020년 11월 4일

댓글:

2020년 11월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by