I am writing a code and I am stuck in this problem. I have a summation that goes from 1 to a certain number, but one of the elements in the function including access to row/column zero (which is not allowed in Matlab).
So, for example:
Let say v= 1 : 10
And the function is:
S(v) + M(v) S(v-1)
I need to tell Matlab that if you get S(0)
return a value of Zero
.
So for v=1 we get S(1)For v=2 we get S(2)+M(2) S(1)
For v=3 we get S(3)+M(3) S(2)
And so on.
Is there a way to do that?
Thank you.
Here's what I would have done: (res stores the result of the equation)
res(1)=S(1);
for v=2:10
res(v)=S(v)+M(v).*S(v-1);
end;
What if I have a complex function with ascending and descending index.
The function that I am trying to code is:
S(1) + (S(2)+M(1)*S(1))X + (S(3)+M(1)*S(2)+M(2)*S(1))X^2 + ...
So, you can have it as:
(S(v)+M(1)*S(v-1)+...+M(v-1)*S(1)) X^v-1
If you are using functions, you can return anything you want when zero is passed in.
For matrices, indexing is always one-based. Usually t=0
is represented by A(1)
. So just add one.