recursive function with 2 variables doesnt work

1 visualización (últimos 30 días)
HorizonUnity
HorizonUnity el 19 de Feb. de 2020
Comentada: HorizonUnity el 19 de Feb. de 2020
Hi there,
i try to get a recursive function
and for
for exampble:
i tried this function:
function x = s(p,t)
if t==1
x = p;
else
x = p*s(p,t-1)+(1-p)*(1-s(p,t-1));
end
end
but when i try to call it with
>> x = s(0.75,2)
i would expect something like
x = 0.625
but i get the error
Index in position 2 exceeds array bounds (must not exceed 1).
so i thought maybe i dont need the p in the function. So i tried:
function x = s(t) etc.
and it works for t=1. But when i call the function with
>> x = s(2)
Index exceeds the number of array elements (1).
What am i doing wrong?

Respuesta aceptada

the cyclist
the cyclist el 19 de Feb. de 2020
Editada: the cyclist el 19 de Feb. de 2020
For me, your function works as expected.
Maybe you accidentally also created a variable s in your workspace, and you are trying to access that instead of the function? What do you get from
which -all s
  1 comentario
HorizonUnity
HorizonUnity el 19 de Feb. de 2020
Hey thanks for your answer!
>> which -all s
s is a variable.
you where absolutly right! I deleted the variable und now it works!
Thank you very much!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by