Array operation shift values

조회 수: 3 (최근 30일)
Ali Mukhtar
Ali Mukhtar 2021년 3월 18일
댓글: Walter Roberson 2021년 3월 18일
i have an array of A=[0 0 0 0 0 ] im sending first value "3" array become A=[3 0 0 0 0] ... then when i send next value for e.g. "5" then array should be A=[5 3 0 0 0] and then if "1" A=[ 1 5 3 0 0] . i cant identify command for this purpose

채택된 답변

Matt J
Matt J 2021년 3월 18일
편집: Matt J 2021년 3월 18일
values=[3,5,1];
A=[0 0 0 0 0 ];
for i=1:numel(values)
A=[values(i), A(1:end-1)]
end
A = 1×5
3 0 0 0 0
A = 1×5
5 3 0 0 0
A = 1×5
1 5 3 0 0
  댓글 수: 2
Ali Mukhtar
Ali Mukhtar 2021년 3월 18일
the values coming in array are serially entered and they are random value... not fix
Matt J
Matt J 2021년 3월 18일
It shouldn't matter.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 3월 18일
A = zeros(1,5);
for K = [3 5 1]
A = [K, A(1:end-1)]
end
A = 1×5
3 0 0 0 0
A = 1×5
5 3 0 0 0
A = 1×5
1 5 3 0 0
  댓글 수: 2
Ali Mukhtar
Ali Mukhtar 2021년 3월 18일
the values coming in array are serially entered and they are random value... not fix
Walter Roberson
Walter Roberson 2021년 3월 18일
A = zeros(1,5);
for K = 1:5
A = [randi(9), A(1:end-1)]
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by