Understanding a multidimensional array in Matlab

Viewed 45

I have a figure and I am trying to understand it in Matlab.

I = imread('frame.png');
x = I(:,:,1);
y = I(:,:,2);
z = I(:,:,3);

In the Matlab workspace, the size of I is 480x640x3. I assume that this means this image has a width and height of 480 and 640 respectively, and 3 channels consisting of Red, Green, Blue.

On the other hand, the size of x in the workspace is 480x640, y is 480x640 and z is 480x640.

I am confused as I thought I(:,:,1) means give me all the rows for the first column and so, the size of x in the workspace should have been 480x1 and not 480x640.

1 Answers

I(:,:,1) gives you all the rows and columns of the first (red) channel.

To simplify this:

% this is a 1x4 array
oneDimensionalArray = [1, 2, 3, 4]

% this is a 2x4 array
twoDimensionalArray = [1, 2, 3, 4;
                       5, 6, 7, 8]

% this is a 2x4x2 array
threeDimensionalArray = cat(3,
                            [1, 2, 3, 4;
                             5, 6, 7, 8],
                            [ 9, 10, 11, 12;
                             13, 14, 15, 16]
                           )

I in your example is similar to threeDimensionalArray, except it has a size of 640x480x3 for every row,column and RGB channel.

threeDimensionalArray(:,:,1) will give you

[1, 2, 3, 4;
 5, 6, 7 ,8]

threeDimensionalArray(2,:,1) will give you second row of first 3rd index.

[5, 6, 7 ,8]

This mathworks tutorial has really good examples too: https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html

Related