How to insert vector into a Matrix?
Mostrar comentarios más antiguos
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 comentarios
KALYAN ACHARJYA
el 15 de Jul. de 2019
Sorry, I cant undestand the question.
If possible please illustate with examples, what you have and what you want?
Andrea Brocchi
el 15 de Jul. de 2019
Alex Mcaulley
el 15 de Jul. de 2019
Do you know that you can do it in just one line?:
MatrixFinal = randi([0,9],4,nmax-1)
Andrea Brocchi
el 15 de Jul. de 2019
Respuestas (1)
Alex Mcaulley
el 15 de Jul. de 2019
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 comentarios
Andrea Brocchi
el 15 de Jul. de 2019
Alex Mcaulley
el 15 de Jul. de 2019
In what line? With the above code all rows are different
Andrea Brocchi
el 15 de Jul. de 2019
Alex Mcaulley
el 16 de Jul. de 2019
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.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!