How to insert vector into a Matrix?

Hi, I do a while cycle, into this while i have a method to create a vector with random number (using randi). At the end of this while cycle i have a vector with all the casual number. This while cycle is in turn within another while cycle which is repeated 4 times, but every 4 times in my matrix i have the same values. I show for you my code:
For example: if in 1 cycle i have generate this vector: [1 2 3 4], this same numbers appair also in the other raw of matrix final.
nSim=1
while nSim<5
while t<nmax
Number=randi([0 9],1,1);
Vector(t)=Number;
t=t+1
end
addVector=[Vector];
MatrixFinal( end+1, :) = addVector;
nSim=nSim+1;
end

4 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 15 日
Sorry, I cant undestand the question.
If possible please illustate with examples, what you have and what you want?
Andrea Brocchi
Andrea Brocchi 2019 年 7 月 15 日
sure I'm sorry. I have a cycle in which I generate random numbers through Randi, and then I go to put them in a vector as shown in the code I published. This cycle is in turn within another cycle which causes the previous cycle "nSim" to be repeated times (in this case 4 times). Every time at the end of the cycle (as shown by the code) I tried to put the vector as a new row of the matrix, but all the lines are exactly identical, and it is unlikely that "Randi" always gives me the same lines all the clips . I hope I explained myself better now, thanks in advance. (sorry for my bad english)
Alex Mcaulley
Alex Mcaulley 2019 年 7 月 15 日
Do you know that you can do it in just one line?:
MatrixFinal = randi([0,9],4,nmax-1)
Andrea Brocchi
Andrea Brocchi 2019 年 7 月 15 日
Yes, but i have to do a lot of calculations with the numbers generated (i don't report here the code with the calculations).

サインインしてコメントする。

回答 (1 件)

Alex Mcaulley
Alex Mcaulley 2019 年 7 月 15 日

0 投票

Well, If you want to follow your current code, to obtain what you want yo just need to initialize t before the while:
nSim=1
while nSim<5
t = 1; %This line
while t<nmax
Number=randi([0 9],1,1);
Vector(t)=Number;
t=t+1
end
addVector=[Vector];
MatrixFinal( end+1, :) = addVector;
nSim=nSim+1;
end

4 件のコメント

Andrea Brocchi
Andrea Brocchi 2019 年 7 月 15 日
I have already initialize t before the while, I forgot to write it here on the forum, sorry.
Alex Mcaulley
Alex Mcaulley 2019 年 7 月 15 日
In what line? With the above code all rows are different
Andrea Brocchi
Andrea Brocchi 2019 年 7 月 15 日
I insert before the first while; if I insert in the position that you say give me this error:
Unable to perform assignment because the size of the left side is 1-by-4319 and
the size of the right side is 1-by-8638.
Alex Mcaulley
Alex Mcaulley 2019 年 7 月 16 日
Upload the full code you are using, it is difficult for us to guess without more information. Putting the initialization of t before the first while dosen't give you the desired results because the loop on t is not executed after the first iteration of the first while loop, then the repeated sequence.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2019 年 7 月 15 日

コメント済み:

2019 年 7 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by