フィルターのクリア

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
  1 件のコメント
g
g 2021 年 12 月 16 日
Thanks, now it works

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by