Summing array entries
Summing array entries
(OP)
Hey guys,
This is the opposite of my last question. How do you find sums of consecutive array entries? I need them to be displayed however and therefore cannot use the sume function. By this I mean [4 5 6 7] = [9 11 13]
Ultimately I want to use diff(x).*(sums of y).
This is the opposite of my last question. How do you find sums of consecutive array entries? I need them to be displayed however and therefore cannot use the sume function. By this I mean [4 5 6 7] = [9 11 13]
Ultimately I want to use diff(x).*(sums of y).





RE: Summing array entries
CODE
answer = A(1:end-1) + A(2:end);
--
Dr Michael F Platten
RE: Summing array entries
RE: Summing array entries
M
--
Dr Michael F Platten
RE: Summing array entries
RE: Summing array entries
for i = 1:1:length(array)-1
answer = x(i)+x(i+1)
end
Would this be a correct approach?
RE: Summing array entries
RE: Summing array entries
1) "answer" is being overwritten each time you go through the loop
2) You are using an unnecessary "for" loop
This is pretty basic stuff. I realise you are new to Matlab, but have you tried reading any books on the subject?
M
--
Dr Michael F Platten
RE: Summing array entries
RE: Summing array entries
TTFN
RE: Summing array entries
Take a look at the sum() function. It looks purpose-built for your problem.
RE: Summing array entries
answer = x(i)+x(i+1)
with
answer(i) = x(i)+x(i+1)
OR
answer = x(1:end-1)+ x(2:end)
which is the vector based approach as given by Mickey P