I got these two vectors, with 9 elements each:
>> length(a)
ans =
9
>> length(c)
ans =
9
And this code:
z=0;
s=0;
temp=0;
for K = 1:length(c)
temp=c(K)*a(K);
z=prod(1-a(K+1:end));
s=s+temp*z;
end
The vector's a indexing is done via K itterator. I would expect that index would blow. Am i missing something here?
length(a)
andlength(c)
are the same,K
won't go out of bounds fora
. Perhaps you're thinking of iterators as pointers such as in C++, but in MATLAB we don't have that:K
is simply a number that goes from1:length(c)
, and you're indexing the matricesa
andc
with this number.temp=c(K)*a(K)
line, not thea(K+1:end)
indexing.