MatLab for loop question

1 view (last 30 days)
Todd
Todd on 9 Nov 2013
Commented: Walter Roberson on 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?

Accepted Answer

Walter Roberson
Walter Roberson on 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 Comments
Todd
Todd on 9 Nov 2013
Edited: Todd on 9 Nov 2013
Thank you! Took out the commas and replaced the {} with [] and it worked!
Walter Roberson
Walter Roberson on 9 Nov 2013
For future reference: you could have left in the commas.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!