Using matlab to evolute a simple vector , but not fit the inference
이전 댓글 표시
clear
clc
time = 4;
size = 8;
% initialize.
a = ones(time,size);
a(1,6) = 5;
% if a(i)>1,then a(i)=a(i)-1 and a(i-1)=a(i-1)+1.
% Saved in a new row.
for t = 1:time-1
for i = 2:size
if a(t,i)>1
a(t+1,i-1) = a(t,i-1) + 1;
a(t+1,i) = a(t,i) - 1;
end
end
end
ans = a(:,:)
Notice the code above, and I want to achieve :
1 1 1 1 1 5 1 1
1 1 1 1 2 4 1 1
1 1 1 2 2 3 1 1
1 1 2 2 2 2 1 1
but the answer is:
1 1 1 1 1 5 1 1
1 1 1 1 2 4 1 1
1 1 1 2 3 3 1 1
1 1 2 3 4 2 1 1
Where is wrong?
댓글 수: 1
Jos (10584)
2019년 2월 28일
A tip: do not use names for variables that are also functions, like size ;-)
채택된 답변
추가 답변 (1개)
David Goodmanson
2019년 2월 28일
편집: David Goodmanson
2019년 2월 28일
Hi Darcy,
It appears that the rule you want is
a(t+1,i-1) = a(t+1,i-1) + 1
which does give the result you are looking for. For a(t+1,i-1) This allows the lowering effect due to a(t,i-1)>1 to persist until it is raised by by the effect due to a(t,1)>1.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!