For loop and store output values

i want to make for loop to creat an array
h1=550
h2=0.8*h1
h3=0.8*h3
..
and strore the values from h1 to h_n
i made that
what is the error
clear all
clc
for i=2:5
h(1)=550;
h(i)=0.8*h(i-1);
[a(i)]=[h(i)]
end

2 comentarios

Ameer Hamza
Ameer Hamza el 17 de Oct. de 2020
Editada: Ameer Hamza el 17 de Oct. de 2020
This code runs without any syntax error?
Mohamed Nasser
Mohamed Nasser el 17 de Oct. de 2020
Yes

Iniciar sesión para comentar.

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 17 de Oct. de 2020
Editada: Ameer Hamza el 17 de Oct. de 2020
Try corrects the logical error in your code
clc
h = zeros(1, 5);
h(1)=550;
for i=2:5
h(i)=0.8*h(i-1);
end

2 comentarios

Mohamed Nasser
Mohamed Nasser el 17 de Oct. de 2020
what is the use of this code
h = zeros(1, 5);
Ameer Hamza
Ameer Hamza el 17 de Oct. de 2020
This is pre-allocation: https://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html. It is there to make code efficient.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 17 de Oct. de 2020

Comentada:

el 17 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by