Replace values of a for loop within a matrix

18 次查看(过去 30 天)
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;]

采纳的回答

Walter Roberson
Walter Roberson 2021-12-16
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by