Reputation: 45
i'm trying to write a script that takes the average of the first two numbers and produces a new list.
for example, if i have
a = [1,2,3,4], i want it to produce b = [1.5, 2.5, 3.5 ]
is there anyway of adding the two endpoints in to the loop? So far, I have:
for i=1:m
betwn(i) = (values(i) + values(i+1))/2 %values is a list
if i = m
break
end
end
and it doesn't seem to be working well...
Thanks!!
Upvotes: 0
Views: 1760
Reputation: 16214
a = [1,2,3,4], i want it to produce b = [1.5, 2.5, 3.5 ]
There is no need in loop
b = (a(1 : end - 1) + a(2 : end)) / 2;
Another way
b = a(1 : end - 1) + diff(a) / 2;
Upvotes: 2