Adding value to a vector

조회 수: 1 (최근 30일)
Louis Grainger
Louis Grainger 2021년 3월 9일
댓글: Walter Roberson 2021년 3월 10일
Function for using a 'for' loop to add 5 differing values to the vector h.
Currently the latter input overwrites the previous.
%prompt
for count = 1:5
h = input("Enter height: ");
???
end
incrementally add 1 to the h variable?

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 10일
h = input("Enter height: ");
for count = 1 : 5
all_h(count) = h + count;
end
  댓글 수: 2
Louis Grainger
Louis Grainger 2021년 3월 10일
input isnt incorporated into the the for so it only asks once giving you one input into the vector
Walter Roberson
Walter Roberson 2021년 3월 10일
Yes, it gives you h and incrementally adds 1 to it like you asked for "incrementally add 1 to the h variable?" The for does 5 iterations, and each time a different value is added to h, so that satisfies "using a 'for' loop to add 5 differing values to the vector h". It also fixes the problem "Currently the latter input overwrites the previous."
Thus, my solution appears to satisfy all of the requirements you posted in the question.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by