vector uses info from itself to grow without for cycle
Mostrar comentarios más antiguos
I need to solve this problem without a for loop:
B= (1:20)
A = [];
A(1) = 1/(1+B(1));
for k = 2:length(B)
A(k,1) = (1-B(k)*sum(A))/(1+B(k));
end
i.e. I need to know if it is possible to get information from prebvious calculation to create a vector, but without a for loop. Thanks.
13 comentarios
darova
el 26 de Mzo. de 2020
You code looks weird
gabriele fadanelli
el 26 de Mzo. de 2020
Editada: gabriele fadanelli
el 26 de Mzo. de 2020
gabriele fadanelli
el 26 de Mzo. de 2020
Guillaume
el 26 de Mzo. de 2020
What is the final A that your code should be producing?
gabriele fadanelli
el 26 de Mzo. de 2020
Editada: gabriele fadanelli
el 26 de Mzo. de 2020
Guillaume
el 26 de Mzo. de 2020
Rather than making us guess what you mean, I'll repeat, what is the desired final (20x1) A for your code above? At least give us the first few elements of the sequence. Alternatively, please write the recursion mathematically.
gabriele fadanelli
el 26 de Mzo. de 2020
Walter Roberson
el 26 de Mzo. de 2020
Your code talks about length(S) but it only uses S(1) and no other S element?
gabriele fadanelli
el 26 de Mzo. de 2020
Walter Roberson
el 27 de Mzo. de 2020
Are you looking for a closed form formula that can give A(K) without calculating A(K-1) ?
Are you looking for a recusive function that can calculate A(K) by calling itself, without an explicit loop?
gabriele fadanelli
el 27 de Mzo. de 2020
Walter Roberson
el 27 de Mzo. de 2020
Which of the two?
Recursive functions with no explicit loop are easy for this.
A closed form formula might be difficult.
gabriele fadanelli
el 28 de Mzo. de 2020
Editada: gabriele fadanelli
el 28 de Mzo. de 2020
Respuesta aceptada
Más respuestas (1)
Ameer Hamza
el 26 de Mzo. de 2020
Editada: Ameer Hamza
el 26 de Mzo. de 2020
For the original code in your question. Following is the simplified form.
k = 1:20;
A = 6.^(k-1);
1 comentario
gabriele fadanelli
el 26 de Mzo. de 2020
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!