MATLAB Answers

How can I concatenate 163 matrices into a single matrix with a loop?

1 view (last 30 days)
I have 163 arrays with same sizes, how can i combine these matrices into a single matrix like M(:,:,163).
I can't organize the loop to do;
M(:,:,1)=G1
M(:,:,2)=G2
....
M(:,:,163)=G163
Thanks very much.

  0 Comments

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 27 Nov 2020
Edited: Ameer Hamza on 27 Nov 2020
The first problem here is to name your variable like G1, G2, ..., G163. It is known to be a bad coding practice: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval and this question show why. Although the use of "eval" is usually discouraged, however, you can use it once to store your data in an array
G = eval(['{' strjoin(compose('G%d', 1:163), ',') '}']);
G = cat(3, G{:});

  2 Comments

Gökhan Bilgin
Gökhan Bilgin on 27 Nov 2020
I'm new to coding, so thank you very much for your guidance and your help. And i will consider this tutorial in my future work.

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!

Translated by