In: Computer Science
• In this script, write MATLAB code to create an array A of size 100 × 3. The first column should contain random numbers between 0 and 10. The second column should also contain random numbers between 0 and 10. The third column should contain random integers between 20 and 50. The first two columns represent the x and y coordinates of a map, respectively. The third column represents the height of buildings. For example, if the first row of A is equal to [3.4 4.5 28], this means that there is a building of height 28 at location (3.4, 4.5) in the map. Because array A has 100 rows, there will be 100 buildings located at random points in the map. • Then, save array A in a file using the MATLAB function save. You may select the name of the file yourselves. • Then, plot the first column of array A vs the second column of array A, using ‘*’ (i.e., the points will be plotted as stars) so that you can see the 100 locations of the buildings. • Finally, after you save the array, clear all variables by placing a clear all at the end of the MATLAB script. • In this script, write MATLAB code to first read the file that you saved in Part A. To read the file, use the MATLAB function load. After you read the file, array A should again become available to you. • Then, write code to move the data from array A to a cell array C of size 10 × 10 in the following way: ◦ The element C{1,1} in C should be an array containing all building heights with x coordinates between 0 and 1 AND y coordinates between 0 and 1. ◦ The element C{2,1} in C should be an array containing all building heights with x coordinates between 1 and 2 AND y coordinates between 0 and 1. ◦ The element C{1,2} in C should be an array containing all building heights with x coordinates between 0 and 1 AND y coordinates between 1 and 2. ◦ The element C{2,2} in C should be an array containing all building heights with x coordinates between 1 and 2 AND y coordinates between 1 and 2. ◦ The element C{3,2} in C should be an array containing all building heights with x coordinates between 2 and 3 AND y coordinates between 1 and 2.
% '%%' represents a section. Each section can be independently
run using
% the run section option
%% Part A
% rand(c,r) function returns a matrix of size c x r of random
numbers
% between 0 and 1. So we just multiply the funciton output by 10 to
get
% random integers between 0 an 10
c1 = 10*rand(100,1); % column 1
c2 = 10*rand(100,1); % column 2
% randi([a b], c, r) function returns a matrix of size c x r of
random
% integers between a and b
c3 = randi([20 50],100,1); % column 3
% given 3 column vectors a, b, c the command [a b c] returns a
matrix with
% columns as a, b, c
A = [c1 c2 c3];
% save('filename.mat','A') saves the matrix A in the current
directory under
% the name given by filename
save('filename.mat','A');
% Given a matrix A A(:,i) returns the ith column
% plot(x,y,'*') plots y vs x using '*'
% here we want to plot column 2 vs column 1
plot(A(:,1),A(:,2),'*');
clear all;
%% Part B
% While saving in a file matlab saves the variables as a struct. To
access
% the variable named A first we load the struct saved using
the
% load('filename.mat') command
Astruct = load('filename.mat');
% Now we extract the matrix A from the struct using struct.A
A = Astruct.A;
% Initialise C
% Loop over entries of C
% Decompose A to individual arrays
c1 = A(:,1);
c2 = A(:,2);
c3 = A(:,3);
for i = 1:10
for j = 1:10
% We will use the fact that C(a<A & A<b & c<B
& B<d) gives us the
% values of C where a<A<b and c<B<d
C{i,j} = c3(i-1 <= c1 & c1 < i & j-1 < c2 & j
< c2);
end
end