finding forward direction in matlab

2 views (last 30 days)
HYZ
HYZ on 12 May 2020
Commented: HYZ on 12 May 2020
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!

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 12 May 2020
Edited: KALYAN ACHARJYA on 12 May 2020
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 Comment
HYZ
HYZ on 12 May 2020
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!