Multidimensional arrays in MATLAB

Today I feel like writing something about MATLAB. I am using it for some projects in the Univerisity. Among all other projects, one very important to me is related to Digital Image Processing, as it is the topic of my Bachelor Degree Thesis, which should be ready in a couple of months. Anyway, dealing with images and MATLAB means dealing with arrays. If you have an image you can import it in MATLAB using the command

imread(‘filename.tiff’); %or whatever extension your image has (jpg, bmp, gif,…)

MATLAB will create of course an array out of this but which kind of array? In case of grayscale images it is clear: the resul will just be a matrix with pixel intensities. What about if the input is an RGB image (color image)=? As each pixel is characterized by three components, RGB (Red, Green, Blue), you will get a 3D array of size height x width x 3. You can think about this array like three matrices one behind another. Maybe this image can make it clearer:

Think about this cube, but change the size to height x width x 3.

Setting / Reading the elements of multidimensional arrays

Now we will see briefly how MATLAB implements 3D (and in general, multidimensional) arrays. Let’s call our test variable a. In our pratical case with color images, the way you have to think the matrix is the following:

a = (rows, cols, channel)

where channel=1 means RED, channel=2 means GREEN and channel = 3 means BLUE.

Setting / Reading a single value in a single channel

Let’s image you want to set a single element of this 3D array. For example, in the RGB contest, you want to set the Green component of the pixel which has coordinates (3,2) to 231 (image it is a 8-bit per channel image: all the values of the matrix will be in the range 0-255):


This approach works of course for all the elements of the multidimensional matrix.

Note: If you for example take an empty variable and set: a(100,100,3)=1; , this will create a 100x100x3 matrix, full of zeros and the last element, (100,100,3), equal to one.

Setting / Reading the RGB value of a pixel

Imagine now you want to set/read not only one value of the 3D matrix, but, let’s say, the RGB value for a specific pixel. Imagine you are interested in the pixel which has coordintes (2,2). Then you have to do following:

a(2,2,:)=[100, 200, 255];

here the “:” sign means “use the vector the user is giving me in the direction “channels””. 100 is the RED, 200 is the GREEN and 255 is the BLUE component.

Setting / Reading a whole channel (color)

Another think you maybe need to do is to set a whole plane, let’s say to set the R component for the whole image. This means that you want to set the whole first plane to some values you have in a matrix, which of course has the same size as the image:

a(:,:,1)=[100 120; 200 250]; %In case of a 2×2 image

Very basic stuff, as you can see. Hope this helps somebody.

Multidimensional arrays in MATLAB

3 thoughts on “Multidimensional arrays in MATLAB

  1. On an artistic note, it is fun to experiment with swapping the red, green and blue color planes. As an example, I swapped the red and blue planes in an image of flamingos standing in grass this way:

    a = a(:,:,[3 2 1])

    This had the artistic effect of turning the birds’ normal orange-salmon color to a rich shade of blue, while leaving the grass (mostly in the green plane) almost unchanged.

    Who says MATLAB can’t be fun?

  2. chia says:

    thanks for this explanation! it helped me understand multidimensional arrays better šŸ™‚ I need to learn how to import my 3D images (TIFF) and had no idea where to start!

  3. chandni says:

    i want to read a 3 d matrix to a text file and then read that file to a 3d matrix which command to use and how plz help me i need this

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s