0

I want to build a loop in Matlab where 'i' is the iteration variable and for each 'i' an n*n matrix will be the outcome. my question is how can I tell the program to save those n*n matrices each one individually where each matrix has the name of Ai for example.

thanks a lot.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • Please read the documentation of [Multidimensional Arrays](https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html) – Sardar Usama Apr 29 '22 at 09:08
  • Do ***NOT*** save variables `A1`, `A2`, etc. This is called *dynamic variable naming* and is [bad, very bad practise](https://stackoverflow.com/a/32467170/5211833). It'll break codes, make your program slow, lead to obfuscated errors. Instead, save it as an n-by-n-by-i matrix, as @SardarUsama said. – Adriaan Apr 29 '22 at 12:00
  • 1
    Here are solutions for the case where the matrices have a different size in each iteration: https://stackoverflow.com/questions/40311988/collect-different-dimensional-data-in-a-loop — you can use either of those, or use a single matrix where each iteration you write a slice `out(:,:,i) = compute(i)`. – Cris Luengo Apr 29 '22 at 17:43

0 Answers0