MATLAB: How can add dimensions to 2D matrix to generate 4D matrix

4d

Hi all,
I would like to generate 4D matrix with following dimensions:
dim1 = image height
dim2 = image width
dim3 = color - can be 1 (grayscale), 2 (Red/Blue) or 3 (RGB color components)
dim4 = image - number of distinct images
I have 1024(dim1)X1280(dim2) matrix (four of them; image5, image15, imaage6, image18). I would like to add third and fourth dimension to this matrix to create a 4D matrix.
I can use cat(4,image5,image15,imaage6,image18) to add images as another dimension. But how can I add the color dimension (dim3), which should be 1 (grayscale)?
Thanks a lot!

Best Answer

When you use
cat(4,image5,image15,imaage6,image18)
if the image5, image15, imaage6, image18 are all 2D matrices, then the result will have length 1 in the third dimension.
Remember, every 2D array, M x N, is also a 3D array, M x N x 1, and a 4D array, M x N x 1 x 1, so when you use cat(4,....) of p (=4) images, you are putting together the M x N x 1 arrays along the 4th dimension, forming M x N x 1 x p (M x N x 1 x 4 in this case.)