Adding value to a vector

2 visualizaciones (últimos 30 días)
Louis Grainger
Louis Grainger el 9 de Mzo. de 2021
Comentada: Walter Roberson el 10 de Mzo. de 2021
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Mzo. de 2021
h = input("Enter height: ");
for count = 1 : 5
all_h(count) = h + count;
end
  2 comentarios
Louis Grainger
Louis Grainger el 10 de Mzo. de 2021
input isnt incorporated into the the for so it only asks once giving you one input into the vector
Walter Roberson
Walter Roberson el 10 de Mzo. de 2021
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by