Effacer les filtres
Effacer les filtres

MatLab for loop question

2 vues (au cours des 30 derniers jours)
Todd
Todd le 9 Nov 2013
Hey guys, probably a simple question but I'm new at this and pretty confused.
So I'm trying to raise a matrix to the powers of 1,2,3,4,5,10,20,25,50,and 75, so I wrote
for t={1,2,3,4,5,10,20,25,50,75}
(A^t)
end
But this is only raising the matrix to the 75th, how do I do this?

Réponse acceptée

Walter Roberson
Walter Roberson le 9 Nov 2013
That code will error out when it first tries to raise the matrix to a power. When you give a cell array of values as the list of values for a "for" loop, the loop variable is set to a cell containing each element in the list, so you would be trying to first raise A to {1} rather than to 1.
If you had used [] instead of {}. the code shown would raise to each power and display the result, but would not do anything with the results other than to display them.
Perhaps your code looks different than what you posted?
  2 commentaires
Todd
Todd le 9 Nov 2013
Modifié(e) : Todd le 9 Nov 2013
Thank you! Took out the commas and replaced the {} with [] and it worked!
Walter Roberson
Walter Roberson le 9 Nov 2013
For future reference: you could have left in the commas.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical 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!

Translated by