How to store values in a vector?

I want to define an empty vector, and then fill it up with some values, the result of a for loop, such as,
function test2()
C = 5
F = 9
for i = 1:3
A = 1+3
B = 3+4
C = 5+6
end
end
The result of this function is 11 values, I need to put them in a vector where its size is 11. the result is: C= 5 F= 9 A = 4 B = 7 C = 11 A = 4 B = 7 C = 11 A = 4 B = 7 C = 11
I need that all those values are stored in a vector.
Vet= 5 9 4 7 11 4 7 11 4 7 11
I would be very grateful if you could help me. Thans.

1 commentaire

Stephen23
Stephen23 le 29 Juil 2016
Modifié(e) : Stephen23 le 29 Juil 2016
@Smache Meriem: your example is far to specific to be useful. Please explain what you are actually trying to achieve. For example, nothing changes inside your loop, so it is not clear why you bother having a loop at all. Nor is it clear why you don't just define the vector using the values that you have given.

Connectez-vous pour commenter.

Réponses (1)

Adam
Adam le 29 Juil 2016

0 votes

Vet = [5 9 repmat( [1+3, 3+4, 5+6], [1 3] )]
will give you that specific vector. For a more general case it depends what you want to do though.

8 commentaires

MS
MS le 29 Juil 2016
@Adam, Thank you for your reply, in fact my is more complicated than this example, I posted it in an easier way , so, when I try t modify your answer as I need: vet Vet = [D F repmat( [A, B, C], [1 3] )] I found that is a syntax error, , Invalid call to repmat.
Adam
Adam le 29 Juil 2016
Please give an example in the original question that shows the full complexity of the problem.
There is no point to giving an over-generalised solution to a trivial problem, but if the example you give is too simple then the solution given is likely to be too simple too!
MS
MS le 29 Juil 2016
Dear Adam and Stephane,
That is my real example:
if (NumScenario==2)
round_Plaintext_initial= dec2hex(vet)
byte_Plaintext_initial=round_Plaintext_initial(NumByte)
round_key_intial=dec2hex (key)
byte_key_initial=round_key_intial(NumByte)
vet = zeros (3,3);
for i_test=1:2
disp('***************')
% state_start est le resultat du add_round_key (state, round_key) initiale.
round_middle_start= dec2hex(round (i_test).start)
byte_start= round_middle_start(NumByte)
round_middle_s_box= dec2hex(round (i_test).s_box)
byte_s_box= round_middle_s_box(NumByte)
round_middle_s_row= dec2hex(round (i_test).s_row)
byte_s_row= round_middle_s_row(NumByte)
round_middle_m_col= dec2hex (round (i_test).m_col)
byte_m_col= round_middle_m_col(NumByte)
round_middle_k_sch= dec2hex(round (i_test).k_sch)
byte_k_sch= round_middle_k_sch(NumByte)
vet(:, i_test) = [byte_Plaintext_initial, byte_key_initial, repmat (byte_start byte_s_box byte_s_row byte_m_col byte_k_sch)]
%output= byte_start,byte_s_box,byte_s_row,byte_m_col]
endfor
vet = vet(:)
end
In fact, I am tryin to extractbytes from every operation in my AES, then store them in a vector. I don't put at first the real exemple because I feel that is so difficult to understand the code. I hope that you could help me. Thanks in advance.
MS
MS le 29 Juil 2016
Modifié(e) : MS le 29 Juil 2016
I need that as a result:
vet= (byte_Plaintext_initial, byte_key_initial, byte_start byte_s_box byte_s_row byte_m_col byte_k_sch)
Adam
Adam le 29 Juil 2016
vet = zeros( 1, 12 );
vet(1:2) = [byte_Plaintext_initial, byte_key_initial];
for i_test = 1:2
idxStep = ( i_test - 1 ) * 5;
...
vet( 3 + idxStep ) = round_middle_start(NumByte);
...
vet( 4 + idxStep ) = round_middle_s_box(NumByte);
...
...
vet( 7 + idxStep ) = round_middle_k_sch(NumByte);
...
end
will probably do the job, with the rest of your code in amongst that, but it isn't a nice solution. The problem for that lies before the point you showed really where you already have all these different variable names that you now want to put into a single vector.
MS
MS le 29 Juil 2016
Thank you very much, It is the best solution
A cleaner solution would be to refactor the loop body into its own function:
function out = somegoodfunctionname(i_test, round, NumByte)
%I assume that round is a variable shadowing matlab round function (not a good idea)
round_middle_start= dec2hex(round (i_test).start)
%... rest of code
byte_k_sch= round_middle_k_sch(NumByte)
out = [byte_start, byte_s_box, byte_s_row, byte_m_col];
end
The body of your main function then becomes:
%...
round_key_intial=dec2hex (key)
byte_key_initial=round_key_intial(NumByte)
vet = arrayfun(@(i_test) somegoodfunctionname(i_test, round, NumByte), ...
1:2, 'UniformOutput', false);
vet = [byte_Plaintext_initial, byte_key_initial, vet{:}];
Much cleaner overall
MS
MS le 29 Juil 2016
Thank you very much @ Guillaume

Connectez-vous pour commenter.

Catégories

Produits

Tags

Question posée :

MS
le 29 Juil 2016

Commenté :

MS
le 29 Juil 2016

Community Treasure Hunt

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

Start Hunting!

Translated by