Replace values of a for loop within a matrix

18 visualizaciones (últimos 30 días)
g
g el 16 de Dic. de 2021
Comentada: g el 16 de Dic. de 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;]

Respuesta aceptada

Walter Roberson
Walter Roberson el 16 de Dic. de 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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by