0

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.

3
  • 2
    There are two dimensions being indexed with :. Also, the first dimension is height, the second one is width. Commented Sep 6, 2022 at 0:58
  • Does this mean I am assessing the height and width of each channel? Thank you. Commented Sep 6, 2022 at 1:04
  • I(:,:,1) means all rows and all columns, and the first element along the 3rd dimension. Commented Sep 6, 2022 at 1:05

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the explanation. I would read the tutorial now.
In MATLAB you don't make a 3D array by nesting more brackets. You need to either explicitly cat along the 3rd dimension (as the change I made to your code) or you need to use reshape.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.