The commands
a = magic(3);
b = pascal(3);
c = cat(4,a,b);
produce a 3-by-3-by-1-by-2 array.
Why is the result 3-3-1-2 when the dimension is 4?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Both
aandbare two-dimensional matrices of size 3-by-3. When you concatenate them along a fourth dimension, the intervening third dimension is singleton (i.e. 1). Soc(:,:,1,1)will be your matrixaandc(:,:,1,2)will be your matrixb.Here’s a link to some documentation that may help with understanding multidimensional arrays.
EDIT:
Perhaps it will help to think of these four dimensions in terms that us humans can more easily relate to…
Let’s assume that the four dimensions in the example represent three dimensions in space (
x,y, andz) plus a fourth dimension time. Imagine that I’m sampling the temperature in the air at a number of points in space at one given time. I can sample the air temperature in a grid that comprises all combinations of threexpositions, threeypositions, and onezposition. That will give me a 3-by-3-by-1 grid. Normally, we’d probably just say that the data is in a 3-by-3 grid, ignoring the trailing singleton dimension.However, let’s say that I now take another set of samples at these points at a later time. I therefore get another 3-by-3-by-1 grid at a second time point. If I concatenate these sets of data together along the time dimension I get a 3-by-3-by-1-by-2 matrix. The third dimension is singleton because I only sampled at one
zvalue.So, in the example
c=cat(4,a,b), we are concatenating two matrices along the fourth dimension. The two matrices are 3-by-3, with the third dimension implicitly assumed to be singleton. However, when concatenating along the fourth dimension we end up having to explicitly show that the third dimension is still there by listing its size as 1.