Can someone give an example of a for loop for this problem?: Find the sum of 30 numbers that increments by 3. The starting number is 4. Thanks!

1 view (last 30 days)
Just a simple for loop please. Nothing too complicated. Thank you again.
  2 Comments
Steven Lord
Steven Lord on 7 Dec 2018
In general if you post a question like this that sounds like a homework assignment, even if it isn't a homework assignment, I recommend posting what you've tried and asking for help improving and/or fixing it. The problem might be as simple as a typo in your code, and seeing that code may allow someone to respond within seconds with the easy fix.
From your question itself, I'm guessing you're fairly new to MATLAB. If that's right, consider going through the free MATLAB Onramp course in the Tutorial section on the Support page if you haven't already. [Click Support in the bar at the top of this page.] It will teach you some of the basics of MATLAB and perhaps fill in any gaps in what you've learned so far.
TSmith
TSmith on 10 Dec 2018
Ok. Thank you. Yes, I'm new to Matlab and just completed an intro course to it. I've understood everything so far except for and while loops. Thanks for your advice.

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 7 Dec 2018
x = sum(4:3:(4+3*(30-1)))
  8 Comments
John D'Errico
John D'Errico on 10 Dec 2018
Edited: John D'Errico on 10 Dec 2018
@Madhan - don't worry about upvotes anyway. Reputation is not important, and there is no race to the top. Your responses are appreciated, improving the site with your presence, regardless of an upvote or not..

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 7 Dec 2018
Read about sum() and cumsum() loops are superfluous in this case
  9 Comments

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!