5 Write some nested for
loops
In MATLAB open the script named labG.m
.
Recall that to loop over all of the elements of a matrix we use a nested pair of for loops. Suppose we have a matrix A; we can loop over the elements of the matrix from left to right one row at a time using the following pair of loops:
[rows, cols] = size(A);
for r = 1:rows
for c = 1:cols
% do something with A(r, c) here
end
end
Answer the following questions in your script.
(a) Fill in the values of a matrix row by row
The script creates a matrix A
having a random number of rows and columns all where all of the elements are equal to zero.
Using a pair of nested for loops, complete the script so that elements have the values 1, 2, 3, …, N where N = (the number of rows of A
) * (the number of columns of A
). For example, if A
has size 5 x 3 then your script should set the elements of A
to:
[ 1 2 3;
4 5 6;
7 8 9;
10 11 12;
13 14 15]
If A
has size 2 x 4 then your script should set the elements of A
to:
[ 1 2 3 4;
5 6 7 8]
(b) Fill in the values of a matrix column by column
If we switch the order of the two loops in the previous question then we end up visiting the elements of A
from top to bottom one column at a time:
[rows, cols] = size(A);
for c = 1:cols
for r = 1:rows
% do something with A(r, c) here
end
end
In the script below, you are given a matrix B
having a random size and all elements equal to zero. Using a pair of nested for loops, complete the script so that elements have the values 1, 2, 3, …, N where N = (the number of rows of A
) * (the number of columns of A
). For example, if B
has size 5 x 3 then your script should set the elements of B
to:
[ 1 6 11;
2 7 12;
3 8 13;
4 9 14;
5 10 15]
If B
has size 2 x 4 then your script should set the elements of B
to:
[ 1 3 5 7;
2 4 6 8]
(c) Compute the average value of the elements in a matrix
In a grayscale image the average gray level in the image is sometimes a useful statistic for simple image processing problems.
In MATLAB, load an example grayscale image like so:
I = double(imread('cameraman.tif')) / 255;
imshow(I);
The image I
is a grayscale image where the value of each pixel is a gray level between 0 and 1.
In the script labG
, use a pair of nested loops to loop over the pixels of I
and compute the average gray level in the image. Store the value of the average gray level in a variable named avg_gray
.
(d) Computing the center of mass of an image mask
Suppose that we have a grayscale image I
. An image mask for I
is a matrix having the same size as I
where each element is either a 0
or a 1
. A mask defines a region of interest (ROI). Mask values of 1
indicate the image pixel belongs to the ROI. Mask pixel values of 0
indicate the image pixel is part of the background.
In MATLAB, type the following:
I = double(imread('pout.tif')) / 255;
imshow(I);
Suppose that we are interested in the region around the child’s head. We can define a mask that selects the region around the child’s head like so:
mask = zeros(size(I)); % a matrix of zeros the same size as I
mask(10:130, 55:175) = 1; % ROI rows 10-130 and columns 55-175
We can apply the mask to I
to produce a new image J
containing only the ROI like so:
J = mask .* I;
figure
imshow(J);
In this example, the mask is rectangular, but in practice the mask can have any shape.
The center of mass of a mask is defined by the following two values:
cm_row = average row number of all of the mask values that are equal to 1
cm_col = average column number of all of the mask values that are equal to 1
In the script labG
, you are given a random sized mask. Complete the script to compute the values of the two variables cm_row
and cm_col
. Use a pair of nested loops to loop over all elements of the mask. In the inner most loop, check if the mask element at the current row and column is equal to 1
; if it is then do the following:
- add the current row number to
cm_row
- add the current column number to
cm_col
- add
1
ton
Observe that n
is the number of pixels that are in the mask.
After both loops, divide cm_row
by n
and cm_col
by n
to get the center of mass of the mask.