필터 지우기
필터 지우기

finding forward direction in matlab

조회 수: 4 (최근 30일)
HYZ
HYZ 2020년 5월 12일
댓글: HYZ 2020년 5월 12일
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!

답변 (1개)

KALYAN ACHARJYA
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.
  댓글 수: 1
HYZ
HYZ 2020년 5월 12일
The fstart I would like to have is vector [1 2] as they are the start of ascending order. but the vector I got is [5 7]. Could you help me check why my if condition is wrong? Thanks

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by