How do i create a 3D Matrix?

372 views (last 30 days)
Robert
Robert on 2 Oct 2014
Commented: Steven Lord about 14 hours ago
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!
  3 Comments
Robert
Robert on 2 Oct 2014
Sorry, I'm very much a matlab newbie. By Workspace files i mean 12 individual matrices. Loops are a little beyond me still. Could you provide an example code?
Thanks for taking the time to assist me
Stephen23
Stephen23 on 2 Oct 2014
Edited: Stephen23 on 2 Oct 2014
How do you generate these matrices?
(I know it might seem unrelated, but trust me...)

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 2 Oct 2014
Edited: Stephen23 on 2 Oct 2014
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.
  2 Comments
Gaia Gbola
Gaia Gbola on 9 Mar 2020
Hello,
I am trying to stack a series of png images, by using a for loop. Could I get help? I keep getting an error by using this code:
clc;clear all;
%A = [imread('frame-3.png') ; imread('frame-4.png');];
for i=1:2456
D = {imread('frame-',(i),'.png')};
Z = cat(3,D{:})
end
imshow(Z)
Stephen23
Stephen23 on 9 Mar 2020
Edited: Stephen23 on 9 Mar 2020
@Gaia Gbola : try something like this:
P = 'absolute/relative path to where the files are saved';
N = 2456;
C = cell(1,N);
for k = 1:N
F = sprintf('frame-%u.png',k);
C{K} = imread(fullfile(P,F));
end
A = cat(4,C{:}) % pick a suitable dimension

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 2 Oct 2014
Edited: Stephen23 on 2 Oct 2014
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
Hannane about 1 hour ago
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
Steven Lord
Steven Lord about 1 hour ago
It works fine here in Answers.
A(:,:,1) = [1 2; 3 4];
A(:,:,2) = [5 6; 7 8];
A(:,:,3) = [9 10; 11 12]
A =
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];
Unable to perform assignment because the size of the left side is 3-by-3 and the size of the right side is 2-by-2.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!