for loop to create a vector V of the squares

14 views (last 30 days)
Use a for loop to create a vector V of the squares of the numbers -25 through 25.
I am looking for some help to get me started writing the code and help writing it itself. I am unsure of where to even start with this problem. Thanks in advance.

Accepted Answer

the cyclist
the cyclist on 16 Sep 2019
Because this is a very basic MATLAB question, and you don't even know how to start, I would recommending watching the MATLAB Onramp tutorial.
  3 Comments
the cyclist
the cyclist on 16 Sep 2019
It's a conundrum, because doing this calculation using a for loop is very "anti-MATLAB". "Making a vector of the squares" leaps right to the answer, and can be done in one line without a for loop -- and is actually the best way to do this.
But since your instructor is presumably trying to teach you about for loops ...
I suggest that you first decide on your algorithm, without writing any MATLAB code. Write down the list of steps. The first step might be "create an object with all the values from -25 to 25", for example. Then, convert those steps into MATLAB code.
timetry2
timetry2 on 16 Sep 2019
So, would my code look like this:
result = [];
for i = -25:25
sum = i^2
result = [result sum];
end
Thanks so much for the help!

Sign in to comment.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!