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 个评论
Mario Malic
2020-11-4
编辑:Mario Malic
2020-11-4
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
2020-11-4
编辑:Maria Imdad
2020-11-4
Stephen23
2020-11-4
'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.
J. Alex Lee
2020-11-4
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
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
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 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!