MATLAB: How do i create a 3D Matrix

3d

I have 12 workspace files all 35×43. I want to make them into one 35x43x12 matrix. What is the best way to do so?
Thanks in advance for any help!

Best Answer

If these matrices are existing variables in your workspace, you can concatenate them on the dimension of your choice:
>> A = [1,2;3,4];
>> B = [5,6;7,8];
>> C = [9,10;11,12];
>> Z = cat(3,A,B,C)
Note that in MATLAB it is often easier, faster and neater to keep all data together, and use vectorization to perform operations on the array data In this case, this might mean keeping all of the arrays in one cell array:
>> D = {first_matrix,second_matrix,...};
>> Z = cat(3,D{:})
OR it might even be better to define the data in one array to start with, rather than in twelve separate matrices. Good data planning in MATLAB makes a world of difference to how easy (and readable!) your code will be.