Borrar filtros
Borrar filtros

finding forward direction in matlab

4 visualizaciones (últimos 30 días)
HYZ
HYZ el 12 de Mayo de 2020
Comentada: HYZ el 12 de Mayo de 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!

Respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 12 de Mayo de 2020
Editada: KALYAN ACHARJYA el 12 de Mayo de 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 comentario
HYZ
HYZ el 12 de Mayo de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by