×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Summing array entries

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).

RE: Summing array entries

CODE

A = [4 5 6 7];
answer = A(1:end-1) + A(2:end);
M

--
Dr Michael F Platten

RE: Summing array entries

It looks to me like you are doing numerical integration.  If this is the case, take a look at Matlab's existng trapz() function.

RE: Summing array entries

I don't think so. trapz returns a single value and cumtrapz would not give the required answer.

M

--
Dr Michael F Platten

RE: Summing array entries

Let's wait and see.  If the next question is: "Is there a way to sum all the elements of a vector and divide by two?" then I win!

RE: Summing array entries

(OP)
Guys I tried
for i = 1:1:length(array)-1
answer = x(i)+x(i+1)
end

Would this be a correct approach?

RE: Summing array entries

(OP)
*replace x with array*

RE: Summing array entries

No. For 2 reasons.

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

(OP)
Yea Mike, im reading a MATLAB intro book for engineers. Unfortunately Im just beginning to program this week. BTW, your way definitely worked. So what you are saying is a for loop only gives one answer after many repetitions of mathematical calculation? Thanks

RE: Summing array entries

No, he's saying YOUR loop produces that result.

TTFN



RE: Summing array entries

JohnKGH,

Take a look at the sum() function.  It looks purpose-built for your problem.

RE: Summing array entries

replace
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources