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

1 view (last 30 days)
Gökhan Bilgin on 27 Nov 2020
Commented: Ameer Hamza on 27 Nov 2020
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.

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{:});

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.
Ameer Hamza on 27 Nov 2020
I am glad to be of help!