4.7 Exercise

4.7.1 Exercises for Script A

  1. Create a 4 × 8 matrix of randomly generated numbers.

  2. Loop through all rows and columns, and test whether each element is greater than 0.5.

  3. Report the results of the test along with the value of the matrix element and its row-column position. For example, your Matlab script should print The 3rd row and 8th column has a value of 0.42345 and is not bigger than 0.5 .

  4. Make sure to add exceptions to print out 1st, 2nd, and 3rd, instead of 1th, 2th, and 3th.

  5. Put this code into a separate function that you can call from the command line with two inputs, corresponding to the number of rows and the number of columns of the matrix.

clear; clc; close all;
%% 4.7.1 Exercises for Script A
matA = GreaterThanPointFive(4,8);

% 生成一个 rowx × colx 的0~1随机数矩阵,并输出各行各列上的元素及其与0.5的大小关系
function matx = GreaterThanPointFive(rowx, colx)
matx = rand(rowx,colx);
NoString = ['th';'st';'nd';'rd';'th';'th';'th';'th';'th';'th'];
for rowi = 1:rowx
for coli = 1:colx
if matx(rowi,coli) > 0.5
disp([ 'The ' num2str(rowi) NoString(mod(rowi,10)+1,:) ' row and ' num2str(coli) NoString(mod(coli,10)+1,:) ' column has a value of ' num2str(matx(rowi,coli)) ' and is bigger than 0.5']);
else
disp([ 'The ' num2str(rowi) NoString(mod(rowi,10)+1,:) ' row and ' num2str(coli) NoString(mod(coli,10)+1,:) ' column has a value of ' num2str(matx(rowi,coli)) ' and is not bigger than 0.5']);
end
end
end
end

4.7.2 Exercises for Script B

  1. Import and plot the picture of Amsterdam that comes with the online Matlab code.

  2. On top of the picture, plot a thick red line from “ Nieuwmarkt ” (near the center of the picture) to “ Station Amsterdam Centraal ” (near the top of the picture).

  3. Plot a magenta star over the Waterlooplein metro station (a bit South of Nieuwmarkt).

  4. Find the maximum value on each color dimension (red, green, or blue) and plot a circle using that color. There may be more than one pixel with a maximum value; if so, pick one pixel at random.

clear; clc; close all;
%% 4.7.2 Exercises for Script B

% 6. Import and plot picture
amsterdamImg = imread("amsterdam.bmp");
figure
imagesc(amsterdamImg);
axis image
axis on
grid on
grid minor

% 7. plot a thick red line
line([367,395],[325,75],'Color','red','LineWidth',2);

% 8. plot a magenta star
hold on;
plot(375,490,'h','MarkerFaceColor','m','MarkerSize',15);

% 9.find maximum value on each color dimension and plot a circle using it
% 分别提取RGB数值
R = amsterdamImg(:,:,1);
G = amsterdamImg(:,:,2);
B = amsterdamImg(:,:,3);
% 找到最大的RGB值
R_max = max(R);
G_max = max(G);
B_max = max(B);
% 找到最大值所在位置
[R_maxRow, R_maxCol] = find(R==R_max);
[G_maxRow, G_maxCol] = find(G==G_max);
[B_maxRow, B_maxCol] = find(B==B_max);
% 随机选择一个索引
R_idx = randi(length(R_maxRow));
G_idx = randi(length(G_maxRow));
B_idx = randi(length(B_maxRow));
% 获取坐标
R_position = [R_maxRow(R_idx), R_maxCol(R_idx)];
G_position = [G_maxCol(G_idx), G_maxCol(G_idx)];
B_position = [B_maxCol(B_idx), B_maxCol(B_idx)];
% 在图像上最大值位置绘制圆圈
hold on;
plot(R_position(1),R_position(2),'o', 'MarkerFaceColor', 'r','MarkerSize',10);
plot(G_position(1),G_position(2), 'o', 'MarkerFaceColor', 'g','MarkerSize',10);
plot(B_position(1),B_position(2), 'o', 'MarkerFaceColor', 'b','MarkerSize',10);
hold off;

image-20240814164819625

4.7.3 Exercises for Script C

  1. From the function you wrote for exercise 5, generate a 32 × 3 number matrix in which the three numbers in each row correspond to the row, column, and result of the test (1 for bigger than 0.5; 0 for smaller than 0.5).

  2. Write this 32 × 3 matrix to a text file that contains this matrix along with appropriate variable labels in the first row. Make sure this file is tab-delimited and readable by a spreadsheet software such as Microsoft Excel or Open Office Calc.

% 运行该代码前请先运行Exercise_04_A

matC = NaN(32,3); % 第一、二列为matA的行、列坐标,第三列为该坐标上的元素是否大于0.5
countRowi = 1;

for rowi = 1:size(matA,1)
for coli = 1:size(matA,2)
matC(countRowi,1) = rowi;
matC(countRowi,2) = coli;
if matA(rowi,coli) > 0.5
matC(countRowi,3) = 1;
else
matC(countRowi,3) = 0;
end
countRowi = countRowi + 1;
end
end

% 将matC写入txt文件
fileC = fopen('data_output_04C.txt','w');

% variable labels
variable_labels = {'row';'column';'result'};
% 写入第一行变量名
for vari=1:length(variable_labels)
fprintf(fileC,'%s\t',variable_labels{vari});
end

% 换行
fprintf(fileC,'\n');

for datarowi=1:size(matC,1)
for columni=1:size(matC,2)
fprintf(fileC,'%g\t',matC(datarowi,columni));
end
fprintf(fileC,'\n');
end

fclose(fileC);