views:

147

answers:

1

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?

+1  A: 

Both a and b are 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). So c(:,:,1,1) will be your matrix a and c(:,:,1,2) will be your matrix b.

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, and z) 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 three x positions, three y positions, and one z position. 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 z value.

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.

gnovice
It's still too abstract to understand,and seems it's much easier when cat along 3rd dimension? Can you elaborate what you mean by `When you concatenate them along a fourth dimension, the intervening third dimension is singleton` ?
Gtker
@Runner: I tried to give a more intuitive example of four-dimensional data. I hope it is a little less abstract now.
gnovice