Victor
Victor

Reputation: 1205

Matrix naming on loop

I'm writing a piece of code where I can define a number of matrices that will be generated and then a loop generates them. The piece where I'm struggling is on the "naming" of the matrices, since I want to name as follows: matrix1; matrix2; etc.

The code below is what I've got so far:

matrices_to_generate = 3;

for i=1:matrices_to_generate
  ['matrix' i] = rand(2,2);
end;

Upvotes: 0

Views: 54

Answers (1)

Pieter12345
Pieter12345

Reputation: 1789

Using assignin, you can write a variable to the workspace with a custom name.

for i=1:matrices_to_generate
  matrix = rand(2,2);
  assignin('base', strcat('matrix', num2str(i)), matrix);
end;

Upvotes: 2

Related Questions