Borrar filtros
Borrar filtros

MatLab for loop question

2 visualizaciones (últimos 30 días)
Todd
Todd el 9 de Nov. de 2013
Comentada: Walter Roberson el 9 de Nov. de 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?

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by