for loop to create a vector V of the squares

조회 수: 11 (최근 30일)
timetry2
timetry2 2019년 9월 16일
댓글: timetry2 2019년 9월 16일
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.

채택된 답변

the cyclist
the cyclist 2019년 9월 16일
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
the cyclist
the cyclist 2019년 9월 16일
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 2019년 9월 16일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by