finding forward direction in matlab
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi,
I want to write a script for separate forward and backward directions for animal position in a linear track. This is simplified vector.
a = [1 3 5 7 9 10 8 6 4 3 2 3 5 6 7 8 9 7 5 3 2 1];
m = length(a);
for i = 2: m-1
if a(i) >= 1 && a(i-1) > i && a(i+1) > a(i)
fstart (i) = a(i-1);
end
end
for j = 2: m-1
if a(j+1) <= 10 && a(j+2) >= a(j+1) && a(j) < a(j+1)
fend (j) = a(j-1);
end
end
The two vectors I want to create is fstart = [1 2] and fend = [10 9] so that later I can combine to have forward vectors [1 3 5 7 9 10] and [ 2 3 5 6 7 8 9].
Please advise me where is wrong.
Thanks in advance!
댓글 수: 0
답변 (1개)
KALYAN ACHARJYA
2020년 5월 12일
편집: KALYAN ACHARJYA
2020년 5월 12일
Please use the another index varibale name inside if statement, like
k=1;
for i=2: m-1
if a(i)>=1 && a(i-1)>i && a(i+1)>a(i)
fstart(k)=a(i-1);
k=k+1;
end
end
So that it avoides those extra zero ( MATLAB fills zero in undefined index value of the array). Rest, just use the correct conditional statement in if condition.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!