Replace values of a for loop within a matrix

18 vues (au cours des 30 derniers jours)
g
g le 16 Déc 2021
Commenté : g le 16 Déc 2021
Good morning,
I have a matrix of zeros ( 6 x 1 ), I would like to insert inside the last row of this matrix the first value that is generated inside the for loop.
Then I would like that the second value generated the second time (in the for loop) will be placed in the last row of the matrix and the value that was previously in the last row of the array will be placed in the penultimate row, and so on (the loop is repeated 48 times, so I would like the process to be repeated without changing the size of the matrix). how can I do this?
thank you.
for example:
A=
[0;
0;
0;
0;
0;
0;]
firstelementgenerated=1; % in the for loop
A=
[0;
0;
0;
0;
0;
1;]
secondelementgenerated=2;
A=
[0;
0;
0;
0;
1;
2;]
%and so on and after 6 times I want a matrix like this:
A=
[1;
2;
3;
4;
5;
6;]
newgeneratedvalue=7;
A=
[2;
3;
4;
5;
6;
7;]

Réponse acceptée

Walter Roberson
Walter Roberson le 16 Déc 2021
A = zeros(6,1);
for K = 1 : 7
A = [A(2:end); A(end)+1]
end
A = 6×1
0 0 0 0 0 1
A = 6×1
0 0 0 0 1 2
A = 6×1
0 0 0 1 2 3
A = 6×1
0 0 1 2 3 4
A = 6×1
0 1 2 3 4 5
A = 6×1
1 2 3 4 5 6
A = 6×1
2 3 4 5 6 7
  1 commentaire
g
g le 16 Déc 2021
Thanks, now it works

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by