loop iteration in matlab
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have the following polynomial
F(x) = 1^5 + (1^5 + 2^5)+ (1^5 + 2^5 + 3^5) + ... + (1^5 + 2^5 + 3^5 + 4^5 + ... + x^5)
How can I write a loop iteration for this polynomials?
1 commentaire
Stephen23
le 20 Nov 2020
Why do you need a loop?
1^5 + (1^5 + 2^5) + (1^5 + 2^5 + 3^5) + (1^5 + 2^5 + 3^5 + 4^5)
sum(cumsum((1:4).^5))
Réponse acceptée
Plus de réponses (1)
Rik
le 20 Nov 2020
I would suggest not using a loop:
F=@(x) sum( ( (1:x).^5 ).*(x:-1:1) );
2 commentaires
Rik
le 20 Nov 2020
Yes, and the code posted by Stephen in a comment is also vectorized.
There are probably some exceptions I'm not thinking of, but if you don't see a loop (or cellfun/arrayfun) the code is vectorized.
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!