필터 지우기
필터 지우기

Replace values of a for loop within a matrix

조회 수: 8 (최근 30일)
g
g 2021년 12월 16일
댓글: g 2021년 12월 16일
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에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by