for loop to create a vector V of the squares

11 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

the cyclist
the cyclist il 16 Set 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 Commenti
the cyclist
the cyclist il 16 Set 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 il 16 Set 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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by