Tinashe Mutsvangwa
Tinashe Mutsvangwa

Reputation: 127

Sequential variables with for loop matlab

I have a 42 variables of same length and in sequence

e.g. d_reshaped1 to d_reshaped42.

Each variable has 3 rows with 42 elements. I would like to combine all the first rows in each of the 42 variables to a single 42 by 42 matrix but my dynamic programming skills in Matlab are miserable.

Can someone assist?

Upvotes: 1

Views: 136

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112769

If you really have those variables, you need to use eval, which is generally not advised:

result = NaN(42,42);
for k = 1:42
    eval(['result(k,:) = d_reshape' num2str(k) '(1,:)'])
end

You should consider using a higher-order structure to store all those variables together, such as a cell array or a 3D array.

Upvotes: 1

Related Questions