How can I store the result after each iteration in a row vector?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Sakshi Koundal
el 10 de Abr. de 2021
Comentada: Sakshi Koundal
el 11 de Abr. de 2021
Below is the sum equation which is of similar form that I need to write in my Objective Function.
I have written the following code using two 'for' loops, it's working fine but the final answer Asum(As) it is storing is only of the last iteration.
Ad = [1 2];
Ac = [1 2 3 4];
for i=1:lenght(Ac)
for j=1:length(Ad)
As=Ad(j)+Ac(i)
end
end
whereas I need the result in the form of a row vector as shown below:
As = [2 3 3 4 4 5 5 6]
Kindly help, thanks
2 comentarios
Respuesta aceptada
David Fletcher
el 10 de Abr. de 2021
A quick hack would be:
Ad = [1 2];
Ac = [1 2 3 4];
As=[];
for i=1:length(Ac)
for j=1:length(Ad)
As=[As Ad(j)+Ac(i)]
end
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!