How would you add every third number, starting with 1, under 100 and create an array?

Answers (2)

Try this:
% Create a 1-D vector:
% Every third number starting with 1 up to but not exceeding 100.
v = 1 : 3 : 100
% Do the addition
theSum = sum(v)

2 Comments

thank you. And in creating and array and performing those vectors operations, would the array be a= [1,4,7...97];?
Hannah, when you get around to actually trying it you'll see this in the command window:
>> v = 1 : 3 : 100
v =
Columns 1 through 19
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55
Columns 20 through 34
58 61 64 67 70 73 76 79 82 85 88 91 94 97 100
Since you didn't want the 100 but wanted to end at 97 and wanted your vector named a instead of v, you'd do
>> a = 1 : 3 : 97

Sign in to comment.

mat = 1:3:100, if you want the vector to be [1,4,7...100] and try mat = 1:3:97 if you want the vector to be [1,4,7,...,97].

Asked:

on 7 Jul 2018

Community Treasure Hunt

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

Start Hunting!