How do i create a 3D Matrix?
Show older comments
I have 12 workspace files all 35x43. I want to make them into one 35x43x12 matrix. What is the best way to do so?
Thanks in advance for any help!
Accepted Answer
More Answers (2)
A typical way to read multiple files in MATLAB would be:
- Preallocate an array of zeros , using the optional argument to make it size 35x43x12.
- Use a loop to read the data from each file, and allocate the values into the array using some indexing . Note that MATLAB indexing is one-based, and is row-major: X(row,col,3rdDim,4thDim,...)
Hannane
on 17 Dec 2025
0 votes
Why I got an ERROR when I try
A(:,:,1) = [1 2; 3 4];
A(:,:,2) = [5 6; 7 8];
A(:,:,3) = [9 10; 11 12];
but it works when I did
>> A = [1,2;3,4];
>> B = [5,6;7,8];
>> C = [9,10;11,12];
>> Z = cat(3,A,B,C)
Can somebody explain why????
1 Comment
It works fine here in Answers.
A(:,:,1) = [1 2; 3 4];
A(:,:,2) = [5 6; 7 8];
A(:,:,3) = [9 10; 11 12]
Please show us the full and exact text of the error message you receive, all the text displayed in red in the Command Window (and if there are any warning messages displayed in orange, please show us those too.) The exact text may be useful and/or necessary to determine what's going on and how to avoid the warning and/or error.
If I had to speculate, I'd guess A is not a 2-by-2 matrix before the first line of code. Or perhaps it's a type that the double matrix on the right side of the equals sign can't be converted into.
A = zeros(3, 3);
A(:,:,1) = [1 2; 3 4];
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!